我在启动另一个时保存我的活动状态时遇到一些问题。它告诉我,它无法暂停活动。这是什么意思?
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SAVED_LOCATION, locationTracker.getCurrentLocation(null));
outState.putSerializable(SAVED_MAP, selectedMap);
outState.putBoolean(SAVED_FOLLOWMODE, mapDisplay.getFollowMode()); //line 280
float[] geoCenter = mapDisplay.getScreenCenterGeoLocation();
if (geoCenter != null) {
outState.putSerializable(SAVED_CENTER, geoCenter);
}
float zoomLevel = mapDisplay.getZoomLevel();
outState.putFloat(SAVED_ZOOMLEVEL, zoomLevel);
if (safetyReminder != null) {
outState.putBundle(SAVED_SAFETY_REMINDER, safetyReminder.onSaveInstanceState());
}
// Save to original intent to have this available at will
getIntent().putExtra(SAVED_INSTANCESTATE, outState);
}
堆栈跟踪:
11-27 23:45:25.968: E/AndroidRuntime(5666): FATAL EXCEPTION: main
11-27 23:45:25.968: E/AndroidRuntime(5666): java.lang.RuntimeException: Unable to pause activity {edu.android.hvz/edu.android.hvz.CustomMaps}: java.lang.NullPointerException
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread.access$2500(ActivityThread.java:125)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.os.Looper.loop(Looper.java:123)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-27 23:45:25.968: E/AndroidRuntime(5666): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 23:45:25.968: E/AndroidRuntime(5666): at java.lang.reflect.Method.invoke(Method.java:521)
11-27 23:45:25.968: E/AndroidRuntime(5666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-27 23:45:25.968: E/AndroidRuntime(5666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-27 23:45:25.968: E/AndroidRuntime(5666): at dalvik.system.NativeStart.main(Native Method)
11-27 23:45:25.968: E/AndroidRuntime(5666): Caused by: java.lang.NullPointerException
11-27 23:45:25.968: E/AndroidRuntime(5666): at edu.android.hvz.CustomMaps.onSaveInstanceState(CustomMaps.java:280)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.Activity.performSaveInstanceState(Activity.java:1036)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
11-27 23:45:25.968: E/AndroidRuntime(5666): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3330)
11-27 23:45:25.968: E/AndroidRuntime(5666): ... 12 more
答案 0 :(得分:1)
在使用mapDisplay
变量之前添加一项检查..
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SAVED_LOCATION, locationTracker.getCurrentLocation(null));
outState.putSerializable(SAVED_MAP, selectedMap);
if(mapDisplay != null) {
outState.putBoolean(SAVED_FOLLOWMODE, mapDisplay.getFollowMode()); //line 280
float[] geoCenter = mapDisplay.getScreenCenterGeoLocation();
if (geoCenter != null) {
outState.putSerializable(SAVED_CENTER, geoCenter);
}
float zoomLevel = mapDisplay.getZoomLevel();
outState.putFloat(SAVED_ZOOMLEVEL, zoomLevel);
}
if (safetyReminder != null) {
outState.putBundle(SAVED_SAFETY_REMINDER, safetyReminder.onSaveInstanceState());
}
// Save to original intent to have this available at will
getIntent().putExtra(SAVED_INSTANCESTATE, outState);
}