Android onCreate()中存在NullPointerException,但onLocationChanged()中不存在NullPointerException
我正在开发一个android应用程序,发现NullPointerException有些奇怪。 RetrieveCampsFromXML()方法可以在onLocationChanged()中运行,但它会在onCreate()中抛出NPE。有人知道为什么吗 如果我将其放入onCreate(): 我是Android新手,stacktrace是我应该提供的吗Android onCreate()中存在NullPointerException,但onLocationChanged()中不存在NullPointerException,android,Android,我正在开发一个android应用程序,发现NullPointerException有些奇怪。 RetrieveCampsFromXML()方法可以在onLocationChanged()中运行,但它会在onCreate()中抛出NPE。有人知道为什么吗 如果我将其放入onCreate(): 我是Android新手,stacktrace是我应该提供的吗 Thread [<3> main] (Suspended (exception NullPointerException)) Vi
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]
Thread[main](挂起(异常NullPointerException))
ViewRoot.draw(布尔)线:1373
ViewRoot.performTraversals()行:1114
ViewRoot.handleMessage(消息)行:1633
ViewRoot(处理程序)。dispatchMessage(消息)行:99
Looper.loop()行:123
ActivityThread.main(字符串[])行:4363
invokenactive(Object,Object[],Class,Class[],Class,int,boolean)行:不可用[本机方法]
调用(对象,对象…)行:521
ZygoteInit$MethodAndArgsCaller.run()行:860
颧骨单位。主(字符串[])行:618
NativeStart.main(字符串[])行:不可用[本机方法]
这是完整的onCreate吗?在onCreate中调用RetrieveCumpsXML时,mContext可能为空。调用RetrieveCumpsXML()后,您必须在onCreate中将McContext设置得更低。由我自己修复。
改变
ArrayList-campusDestinations=new-ArrayList();
到
ArrayList-campusDestinations;
然后,现在可以将retrieveCampsFromXML方法放入onCreate()中
非常感谢谁回答了我的问题!:) 我很抱歉,因为我不小心剪切了相关代码。事实上,mContext已经被实例化了;完成了吗?很抱歉,这是我第一次在这里提问。如何获得正确的堆栈跟踪?
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;
}
final LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location arg0) {
myLocation = arg0;
if (myLocation != null) {
if (firstRunForXML) {
firstRunForXML = false;
campusDestinations = retrieveCampusFromXml(xrpCampus);
}
}
}
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]
ArrayList<Destination> campusDestinations = new ArrayList<Destination> ();
ArrayList<Destination> campusDestinations;