【问题标题】:NullPointerException in savedInstanceState BundlesavedInstanceState 包中的 NullPointerException
【发布时间】:2014-02-25 06:03:50
【问题描述】:

当我的代码尝试访问由 Activity 类的 onSaveInstanceState 方法创建的键/值对中的值时,我收到 NullPointerException。

我设置了断点,我知道我的 Bundle 不为空,它包含对我的键/值的引用。我不明白为什么会收到此运行时错误。这是我的 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);
}

这是我在 onCreate 方法上恢复状态的代码

// 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")));
      } 

我在以 resultsTextView.setText... 开头的行出现运行时错误,这是从调试模式下的断点检索到的 savedInstanceState Bundle 的内容

 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}]

你可以看到我所有的字符串都有一个值,有趣的是我没有在 int 变量(winCount 和 lossCount)上得到 NullPointerException 运行时错误,但我在字符串值上得到它。我很感激任何帮助。

更新:这是来自 log cat 的整个错误日志,我在第 68 行有 resultsTextView.setText...(在 onCreat() 的 else 块内)

 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

这是我的整个 onCreate 方法,因为许多评论员要求查看整个方法。希望对您有所帮助!

@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));

    }

【问题讨论】:

  • 您可以删除 String.valueOf 因为 savedInstanceState.getString 已经返回 String。
  • 你能把异常日志贴在这里吗?
  • @Niko 起初我没有 String.ValueOf 但我遇到了相同的运行时错误,我认为这可能是由于一些奇怪的未匹配导致的,我补充说。无论哪种方式我都没有工作!
  • resultsTextView 你在哪里初始化它你可以发布完整的 onCreate 函数
  • @alkber 我用整个错误日志更新了问题

标签: java android nullpointerexception bundle oncreate


【解决方案1】:

您应该在调用 setText 方法之前初始化您的TextViews。所以 onCreate 应该是这样的:

        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"));
          } 
...

【讨论】:

  • 谢谢它的工作,在你的评论之后,我真的继续这样做了,现在它正在无缝地工作
  • 这与我在回答中提到的相同。任何方式都很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多