我正在開發一個android應用程序,並發現有關NullPointerException的奇怪事情。 方法retrieveCampusFromXml()可以在onLocationChanged()中運行,但它在onCreate()中引發NPE。有人知道爲什麼嗎?onCreate()中的NullPointerException,但不在onLocationChanged()
如果我把它的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()(其中包括大量的自定義對象,如目的地):
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的新手,是像我這樣的堆棧跟蹤uld提供? :
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]
在堆棧跟蹤中沒有關於onCreate(...)的引用;它完成了嗎? – thoredge 2011-03-01 15:40:41
對不起,這是我第一次在這裏提問。我如何獲得正確的堆棧跟蹤? – 2011-03-01 15:54:21