简体   繁体   English

尝试在Android中旋转时保存和还原实例状态时发生NullPointerException

[英]NullPointerException when trying to save and restore instance state on rotation in Android

I'm doing an exercise where the app logs whenever the lifecycle functions are called (onStart, onResume, etc.). 我正在做一个练习,其中每当调用生命周期函数(onStart,onResume等)时,应用程序都会进行记录。 It all works until I try to implement the onSaveInstanceState wherein it tells me that I can't apply .getText() on a null object reference 这一切都有效,直到我尝试实现onSaveInstanceState为止,在其中它告诉我不能对空对象引用应用.getText()

Here is my code 这是我的代码

private TextView creatCount;
private TextView createTime;
private TextView startCount;
private TextView startTime;
private TextView resumeCount;
private TextView resumeTime;
private TextView pauseCount;
private TextView pauseTime;
private TextView stopCOunt;
private TextView stopTime;
private TextView destroyCount;
private TextView destroyTime;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lifecycle);
    creatCount = (TextView)findViewById(R.id.create_count);
    createTime = (TextView)findViewById(R.id.create_time);
    increment(creatCount);
    timeStamp(createTime);

    if (savedInstanceState != null){
        creatCount.setText(savedInstanceState.getString("create_count"));
        createTime.setText(savedInstanceState.getString("create_time"));
        startCount.setText(savedInstanceState.getString("start_count"));
        startTime.setText(savedInstanceState.getString("start_time"));
        resumeCount.setText(savedInstanceState.getString("resume_count"));
        resumeTime.setText(savedInstanceState.getString("resume_time"));
        pauseCount.setText(savedInstanceState.getString("pause_count"));
        pauseTime.setText(savedInstanceState.getString("pause_time"));
        stopCOunt.setText(savedInstanceState.getString("stop_count"));
        stopTime.setText(savedInstanceState.getString("stop_time"));
        destroyCount.setText(savedInstanceState.getString("destroy_count"));
        destroyTime.setText(savedInstanceState.getString("destroy_time"));
    }
}

public void onStart(){
    super.onStart();
    startCount = (TextView)findViewById(R.id.start_count);
    startTime = (TextView)findViewById(R.id.start_time);
    increment(startCount);
    timeStamp(startTime);
}

public void onResume(){
    super.onResume();
    resumeCount = (TextView)findViewById(R.id.resume_count);
    resumeTime = (TextView)findViewById(R.id.resume_time);
    increment(resumeCount);
    timeStamp(resumeTime);
}

public void onPause(){
    super.onPause();
    pauseCount = (TextView)findViewById(R.id.pause_count);
    pauseTime = (TextView)findViewById(R.id.pause_time);
    increment(pauseCount);
    timeStamp(pauseTime);
}

public void onStop(){
    super.onStop();
    stopCOunt = (TextView)findViewById(R.id.stop_count);
    stopTime = (TextView)findViewById(R.id.stop_time);
    increment(stopCOunt);
    timeStamp(stopTime);
}

public void onDestroy(){
    super.onDestroy();
    destroyCount = (TextView)findViewById(R.id.destroy_count);
    destroyTime = (TextView)findViewById(R.id.destroy_time);
    increment(destroyCount);
    timeStamp(destroyTime);
}

public void increment(TextView view){
    String number = view.getText().toString();
    int count = Integer.parseInt(number);
    count++;
    view.setText(String.valueOf(count));
}

public void timeStamp(TextView view){
    String currentTime = DateFormat.getTimeInstance().format(new Date());
    view.setText(currentTime);
}

public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putString("create_count", creatCount.getText().toString());
    savedInstanceState.putString("create_time", createTime.getText().toString());
    savedInstanceState.putString("start_count", startCount.getText().toString());
    savedInstanceState.putString("start_time", startTime.getText().toString());
    savedInstanceState.putString("resume_count", resumeCount.getText().toString());
    savedInstanceState.putString("resume_time", resumeTime.getText().toString());
    savedInstanceState.putString("pause_count", pauseCount.getText().toString());
    savedInstanceState.putString("pause_time", pauseTime.getText().toString());
    savedInstanceState.putString("stop_count", stopCOunt.getText().toString());
    savedInstanceState.putString("stop_time", stopTime.getText().toString());
    savedInstanceState.putString("destroy_count", destroyCount.getText().toString());
    savedInstanceState.putString("destroy_time", destroyTime.getText().toString());

    super.onSaveInstanceState(savedInstanceState);
    }
}

All the default text values are in the xml and set to "0" 所有默认文本值都在xml中,并设置为“ 0”

And here's the logcat 这是logcat

02-02 08:29:26.971 24397-24397/com.example.jmcervantes02.jan_24 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.jmcervantes02.jan_24, PID: 24397
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
                                                                                  at com.example.jmcervantes02.jan_24.LifecycleActivity.onSaveInstanceState(LifecycleActivity.java:115)
                                                                                  at android.app.Activity.performSaveInstanceState(Activity.java:1482)
                                                                                  at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1301)
                                                                                  at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4625)
                                                                                  at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3935)
                                                                                  at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4000)
                                                                                  at android.app.ActivityThread.-wrap25(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1568)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:163)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6342)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

Ok, first let's understand the Activity's life cycle. 好的,首先让我们了解活动的生命周期。 On this link you can check with more details. 在此链接上,您可以查看更多详细信息。 But for now: 但现在:

Act. 法案。 Launched -> onCreate -> onStart -> onResume -> Running... 启动-> onCreate-> onStart-> onResume->运行中...

Tip: Always set View's elements within the onCreate method on activities (or in case of using fragments, within the onCreateView method. 提示:始终在活动的onCreate方法中设置View的元素(或在使用片段的情况下,在onCreateView方法中设置View的元素)。

keep in mind the life cycle above, and look at your "onCreate" method. 请牢记上面的生命周期,并查看您的“ onCreate”方法。 Can you realize that you are calling the .setText methods from TextView without even get their references? 您是否意识到您正在从TextView调用.setText方法,甚至没有获得它们的引用?

    startCount.setText(savedInstanceState.getString("start_count"));
    startTime.setText(savedInstanceState.getString("start_time"));
    resumeCount.setText(savedInstanceState.getString("resume_count"));
    resumeTime.setText(savedInstanceState.getString("resume_time"));
    pauseCount.setText(savedInstanceState.getString("pause_count"));
    pauseTime.setText(savedInstanceState.getString("pause_time"));
    stopCOunt.setText(savedInstanceState.getString("stop_count"));
    stopTime.setText(savedInstanceState.getString("stop_time"));
    destroyCount.setText(savedInstanceState.getString("destroy_count"));
    destroyTime.setText(savedInstanceState.getString("destroy_time"));

Here you are setting text in no-referenced elements. 在这里,您要在无参考元素中设置文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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