Android 앱을 개발 중이며 NullPointerException에 대해 이상한 것을 찾습니다. 동일한 메소드 retrieveCampusFromXml()은 onLocationChanged()에서 실행할 수 있지만 onCreate()에서 NPE를 발생시킵니다. 누군가 왜 그 이유를 알고 있습니까?onLocationChanged()에서 onCreate()가 아닌 NullPointerException
전에서 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);
}
}
}
그리고 안드로이드에 새로운, 나는 쇼 같은 스택 트레이스는 오전 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
죄송합니다. 여기에 질문하는 것이 처음입니다. 올바른 스택 추적을 얻으려면 어떻게해야합니까? –