简体   繁体   中英

Activity fails in onRestoreInstanceState() - Null Pointer Exception

I have an ArrayList of an object. I am trying to save it as mentioned in the below code. When I try to fetch the object in onRestoreInstanceState() I m getting NullPointerException. Kindly point me where the code needs correction

  @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

Because of this line you are not getting data

outState = new Bundle(); 

dont create object evary time..create only when the Bundle is null..so check like this..

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

That too your Memory class must implements Serializable Interface like.

public class Memory implements Serializable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM