onRestoreInstanceState()中的活动失败 - 空指针异常

时间:2014-03-29 11:03:02

标签: android android-activity

我有一个对象的ArrayList。我正在尝试保存它,如下面的代码中所述。当我尝试在onRestoreInstanceState()中获取对象时,我得到NullPointerException。请指出代码需要修正的地方

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState); 
    Log.i("MemoryGame", "onSaveInstanceState");   
    outState = new Bundle(); 
    outState.putSerializable("saveMemory", (Serializable) memory);
  } 

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.i("MemoryGame", "onRestoreInstanceState");    
    ArrayList<Memory> retrievedMemory =  (ArrayList<Memory>) savedInstanceState.getSerializable("saveMemory"); 
    Log.i("MemoryGame", "onRestoreInstanceState : memory " + retrievedMemory.get(0).getPictureName()); 
  }

logcat的

03-29 16:26:58.047: E/AndroidRuntime(16281): FATAL EXCEPTION: main
03-29 16:26:58.047: E/AndroidRuntime(16281): java.lang.RuntimeException: Unable to start activity ComponentInfo{kids.animals.fruits.objects.brain.puzzle.memory.game.free/kids.animals.fruits.objects.brain.puzzle.memory.game.free.MemoryGame}: java.lang.NullPointerException
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.ActivityThread.access$600(ActivityThread.java:138)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.os.Looper.loop(Looper.java:213)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.ActivityThread.main(ActivityThread.java:4787)
03-29 16:26:58.047: E/AndroidRuntime(16281): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 16:26:58.047: E/AndroidRuntime(16281): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 16:26:58.047: E/AndroidRuntime(16281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-29 16:26:58.047: E/AndroidRuntime(16281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-29 16:26:58.047: E/AndroidRuntime(16281): at dalvik.system.NativeStart.main(Native Method)
03-29 16:26:58.047: E/AndroidRuntime(16281): Caused by: java.lang.NullPointerException
03-29 16:26:58.047: E/AndroidRuntime(16281): at kids.animals.fruits.objects.brain.puzzle.memory.game.free.MemoryGame.onRestoreInstanceState(MemoryGame.java:2135)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.Activity.performRestoreInstanceState(Activity.java:900)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1130)
03-29 16:26:58.047: E/AndroidRuntime(16281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
03-29 16:26:58.047: E/AndroidRuntime(16281): ... 11 more

1 个答案:

答案 0 :(得分:1)

由于此行,您无法获取数据

outState = new Bundle(); 

不要创建对象evary time..create只在Bundle为null时才进行..所以请像这样检查..

 @Override
 protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState); 
        Log.i("MemoryGame", "onSaveInstanceState");   
        if (outState==null) {
            outState = new Bundle();
        }
        outState.putSerializable("saveMemory", (Serializable) memory);
      } 

你的Memory类也必须实现Serializable接口,如。

public class Memory implements Serializable