问题:应用程序是从设备中获取经度和纬度的读数。执行此功能的活动第一次运行良好...但是当第二次进入活动时应用程序崩溃,给出以下NullPointerException
:
08-25 19:53:36.517: E/AndroidRuntime(26672): FATAL EXCEPTION: main
08-25 19:53:36.517: E/AndroidRuntime(26672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vaw.selfhelp/com.vaw.selfhelp.SmsActivity}: java.lang.NullPointerException
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.os.Looper.loop(Looper.java:130)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-25 19:53:36.517: E/AndroidRuntime(26672): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:53:36.517: E/AndroidRuntime(26672): at java.lang.reflect.Method.invoke(Method.java:507)
08-25 19:53:36.517: E/AndroidRuntime(26672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-25 19:53:36.517: E/AndroidRuntime(26672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-25 19:53:36.517: E/AndroidRuntime(26672): at dalvik.system.NativeStart.main(Native Method)
08-25 19:53:36.517: E/AndroidRuntime(26672): Caused by: java.lang.NullPointerException
08-25 19:53:36.517: E/AndroidRuntime(26672): at com.vaw.selfhelp.SmsActivity.onCreate(SmsActivity.java:85)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-25 19:53:36.517: E/AndroidRuntime(26672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-25 19:53:36.517: E/AndroidRuntime(26672): ... 11 more
代码:
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
longitude = location.getLongitude(); //this is line 85 which the logcat is referring to
latitude = location.getLatitude();
有人可以帮忙!
答案 0 :(得分:0)
代码可能正常工作,因为你说它只会在第二次返回活动时崩溃。您应该在活动onResume()回调期间重新分配位置对象。