简体   繁体   中英

NullPointerException in savedInstanceState Bundle

I am getting a NullPointerException when my code tries to access the value in a key/value pair created by onSaveInstanceState method of Activity class.

I set break points and I know for fact my Bundle is not null and it contains references to my key/values. I dont understand why I am getting this runtime error. Here are my codes for onSaveInstanceState

@Override
protected void onSaveInstanceState(Bundle outState) {

    int mPoints = winCount;
    int hPoints = loseCount;
    String reTextView = resultsTextView.getText().toString();
    String pTextView = pointsTextView.getText().toString();
    String roTextView = rollTextView.getText().toString();

    outState.putInt("MY_POINTS", mPoints);
    outState.putInt("HOUSE_POINTS", hPoints);
    outState.putString("RESULTS", reTextView);
    outState.putString("POINTS", pTextView);
    outState.putString("ROLL", roTextView);

    super.onSaveInstanceState(outState);
}

and here is my code to restore the state on the onCreate method

// check if app just started or is being restored from memory
      if ( savedInstanceState == null ) // the app just started running
      {
         winCount = 0; 
         loseCount = 0; 
      } 
      else 
      {
         winCount = savedInstanceState.getInt("MY_POINTS");
         loseCount = savedInstanceState.getInt("HOUSE_POINTS");

         resultsTextView.setText(String.valueOf(savedInstanceState.getString("RESULTS")));
         pointsTextView.setText(String.valueOf(savedInstanceState.getString("POINTS")));
         rollTextView.setText(String.valueOf(savedInstanceState.getString("ROLL")));
      } 

I get the runtime error on line that starts with resultsTextView.setText... and here is contents of the savedInstanceState Bundle retrieved from break points in debug mode

 Bundle[{RESULTS=Roll Again, MY_POINTS=2, POINTS=Your Point is 8,
 HOUSE_POINTS=2,
 android:viewHierarchyState=Bundle[{android:Panels=android.util.SparseArray@421c5560,
 android:views=android.util.SparseArray@421c5358,
 android:ActionBar=android.util.SparseArray@421c57f8}], ROLL=You Rolled
 Easy Four}]

as you can see all my strings have a value, the interesting thing is that I dont get the NullPointerException runtime error on int variables (winCount and loseCount) but I am getting it at string values. I appreciate any help.

Update: here is the whole error log from log cat, I have resultsTextView.setText... at line 68 (within the else block on onCreat())

 W/dalvikvm(27797): threadid=1: thread exiting with uncaught exception (group=0x418b6700)
E/AndroidRuntime(27797): FATAL EXCEPTION: main
E/AndroidRuntime(27797): java.lang.RuntimeException: Unable to start activity ComponentInfo{…}: java.lang.NullPointerException
E/AndroidRuntime(27797): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime(27797):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime(27797):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
E/AndroidRuntime(27797):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
E/AndroidRuntime(27797):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
E/AndroidRuntime(27797):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(27797):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(27797):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(27797):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27797):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(27797):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(27797):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(27797):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(27797): Caused by: java.lang.NullPointerException
E/AndroidRuntime(27797):    at app.package.onCreate(AppName.java:68)
E/AndroidRuntime(27797):    at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime(27797):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(27797):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime(27797):    ... 12 more

here is my whole onCreate method, since many commentators requested to see the whole method. Hope it helps!

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

          // check if app just started or is being restored from memory
          if ( savedInstanceState == null ) // the app just started running
          {
             winCount = 0; 
             loseCount = 0; 
          } 
          else 
          {
             winCount = savedInstanceState.getInt("MY_POINTS");
             loseCount = savedInstanceState.getInt("HOUSE_POINTS");

             resultsTextView.setText(savedInstanceState.getString("RESULTS"));
             pointsTextView.setText(savedInstanceState.getString("POINTS"));
             rollTextView.setText(savedInstanceState.getString("ROLL"));
          } 


        die1 = (ImageView) findViewById(R.id.imageView1);
        die2 = (ImageView) findViewById(R.id.imageView2);
        dealButton = (Button) findViewById(R.id.dealButton);
        resetButton = (Button) findViewById(R.id.resetButton);
        resultsTextView = (TextView) findViewById(R.id.resultsTextView);
        myPointsTextView = (TextView) findViewById(R.id.myPointstTextView);
        housePointsTextView = (TextView) findViewById(R.id.housePointsTextView);
        pointsTextView = (TextView) findViewById(R.id.pointsTextView1);
        rollTextView = (TextView) findViewById(R.id.rollTextView);

        dealButton.setOnClickListener(dealButtonListener);
        resetButton.setOnClickListener(resetButtonLinstener);

        //on shake event
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector(new OnShakeListener() {

            @Override
            public void onShake() {
               game(rollDice());
            }
        });


        resultsTextView.setTextColor(Color.BLACK);
        myPointsTextView.setText(String.format("%s", winCount));
        housePointsTextView.setText(String.format("%s", loseCount));

    }

You should initialize your TextView s before calling setText method. So onCreate should be like this:

        setContentView(R.layout.activity);

        die1 = (ImageView) findViewById(R.id.imageView1);
        die2 = (ImageView) findViewById(R.id.imageView2);
        dealButton = (Button) findViewById(R.id.dealButton);
        resetButton = (Button) findViewById(R.id.resetButton);
        resultsTextView = (TextView) findViewById(R.id.resultsTextView);
        myPointsTextView = (TextView) findViewById(R.id.myPointstTextView);
        housePointsTextView = (TextView) findViewById(R.id.housePointsTextView);
        pointsTextView = (TextView) findViewById(R.id.pointsTextView1);
        rollTextView = (TextView) findViewById(R.id.rollTextView);

          // check if app just started or is being restored from memory
          if ( savedInstanceState == null ) // the app just started running
          {
             winCount = 0; 
             loseCount = 0; 
          } 
          else 
          {
             winCount = savedInstanceState.getInt("MY_POINTS");
             loseCount = savedInstanceState.getInt("HOUSE_POINTS");

             resultsTextView.setText(savedInstanceState.getString("RESULTS"));
             pointsTextView.setText(savedInstanceState.getString("POINTS"));
             rollTextView.setText(savedInstanceState.getString("ROLL"));
          } 
...

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