0
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    savedInstanceState.putString("foo", "bar");
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  String myString = savedInstanceState.getString("foo");

  Log.i("debug", "saved data: " + myString);
}

アクティビティでいくつかの値を保持しようとしていますが、次のエラーが表示されます:06-23 23:09:44.038: E/AndroidRuntime(17584): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.johanberntsson.activitytest/se.johanberntsson.activitytest.TestActivity}: java.lang.NullPointerException

ここで何を見逃したのですか?ありがとう

4

1 に答える 1

3

onSaveInstanceState以下のように、savedInstanceState バンドルに値を保存するためのオーバーライド:

public String strfoo="";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);
            //comment this line you only fatch values from savedInstanceState
            String myString = savedInstanceState.getString("foo");
            Log.i("debug", "saved data: " + myString);
            // savedInstanceState.putString("foo", "bar");
            strfoo="bar"; set value here
        }

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          String myString = savedInstanceState.getString("foo");
          Log.i("debug", "saved data: " + myString);
        }

      @Override

      public void onSaveInstanceState(Bundle savedInstanceState) {
         savedInstanceState.putString("foo", strfoo);
         super.onSaveInstanceState(savedInstanceState);

       } 
于 2012-06-23T21:16:47.650 に答える