我正在开发一个Android应用程序并找到一些关于NullPointerException的奇怪之处。 相同的方法retrieveCampusFromXml()可以在onLocationChanged()中运行,但它会在onCreate()中抛出NPE。有人知道为什么吗?
如果我把它放在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()(其中包含大量自定义对象,例如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;
}
如果我把它放在onLocationChanged()中,那一切都没问题:
final LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location arg0) {
myLocation = arg0;
if (myLocation != null) {
if (firstRunForXML) {
firstRunForXML = false;
campusDestinations = retrieveCampusFromXml(xrpCampus);
}
}
}
我是Android新手,是我应该提供的堆栈跟踪吗? :
Thread [<3> main] (Suspended (exception NullPointerException))
ViewRoot.draw(boolean) line: 1373
ViewRoot.performTraversals() line: 1114
ViewRoot.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]
答案 0 :(得分:1)
那是完整的onCreate吗?在onCreate中调用retrieveCampusXml时,mContext可能为null。在调用retrieveCampusXml()之后,您必须在onCreate中进一步设置mContext。
答案 1 :(得分:0)
由我自己修复。 改变
ArrayList<Destination> campusDestinations = new ArrayList<Destination> ();
到
ArrayList<Destination> campusDestinations;
然后可以将方法retrieveCampusFromXml放入onCreate()中。
感谢谁非常回答我的问题! :)