NullPointerException in onCreate(), but not in onLocationChanged()
I am developing an android app and find something weird about NullPointerException. The same method retrieveCampusFromXml() can run in onLocationChanged() but it throws NPE in onCreate(). Does someone know why?
If I put it in onCreate() :
public class XXXX extends Activity {
XmlResourceParser xrpCampus;
ArrayList<Destination> campusDestinations = new ArrayList<Destination> ();
firstRunForXML = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appState = ((MyApp)this.getApplication());
mContext = this;
xrpCampus = getResources().getXml(R.xml.campus);
campusDestinations = retrieveCampusFromXml(xrpCampus);campusDestinations = retrieveCampusFromXml(xrpCampus);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
best = mLocationManager.getBestProvider(criteria, true);
mLocationManager.requestLocationUpdates(best, 1000, 0, mLocationListener);
retrieveCampusFromXml() (which included lots of custom object, e.g. Destination):
private ArrayList<Destination> retrieveCampusFromXml(XmlResourceParser xrp) {
ArrayList<Destination> listOfCampus = new ArrayList<Destination> ();
String tagName = "";
try {
Destination temp=null;
int latXml=0;
int lngXml=0;
int eventType = xrp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
tagName = xrp.getName();
if(tagName.equals("destination")) {
temp = new Destination();
}
}
else if (eventType == XmlPullParser.END_TAG) {
tagName="";
if(xrp.getName().equals("destination")) {
temp.setGeoPoint(latXml, lngXml);
listOfCampus.add(temp);
latXml=0;
lngXml=0;
}
}
else if (eventType == XmlPullParser.TEXT) {
if (tagName.equals("name")) {
temp.setName(xrp.getText());
}
else if (tagName.equals("latitude")) {
latXml = (int) (Double.parseDouble(xrp.getText())*1E6);
}
else if (tagName.equals("longitude")) {
lngXml = (int) (Double.parseDouble(xrp.getText())*1E6);
}
else if (tagName.equals("altitude") && !xrp.getText().equals("undefined")) {
temp.setAltitude(Double.parseDouble(xrp.getText()) );
}
else if (tagName.equals("drawableID") ) {
temp.setDrawable( mContext, Integer.decode(xrp.getText()) );
}
}
eventType = xrp.next();
}
} catch (XmlPullParserException e) {
Log.i("LUN", "XML");
e.printStackTrace();
} catch (IOException e) {
Log.i("LUN", "IO");
e.printStackTrace();
} catch (NullPointerException e) {
Log.i("LUN", "NULL");
e.printStackTrace();
}
return listOfCampus;
}
If I put it in onLocationChanged() like that, everything's ok:
final LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location arg0) {
myLocation = arg0;
if (myLocation != null) {
if (firstRunForXML) {
firstRunForXML = false;
campusDestinations = retrieveCampusFromXml(xrpCampus);
}
}
}
And I am new to Android, is the stacktrace like that I should provide? :
Thread [<3> main] (Suspended (exception NullPointerException))
ViewRoot.draw(boolean) line: 1373
ViewRoot.performTraversals() line: 1114
Vi开发者_高级运维ewRoot.handleMessage(Message) line: 1633
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4363
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 860
ZygoteInit.main(String[]) line: 618
NativeStart.main(String[]) line: not available [native method]
Is that the full onCreate? When you call retrieveCampusXml in onCreate, mContext may be null. You must be setting mContext further down in onCreate after the call to retrieveCampusXml().
Fixed by myself. Change
ArrayList<Destination> campusDestinations = new ArrayList<Destination> ();
to
ArrayList<Destination> campusDestinations;
Then the method retrieveCampusFromXml can be put in onCreate() now.
Thanks to who answered my question very much! :)
精彩评论