我正在做一个练习,每当调用生命周期函数时,app都会记录(onStart,onResume等)。这一切都有效,直到我尝试实现onSaveInstanceState,它告诉我我不能在空对象引用上应用.getText()
这是我的代码
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);
}
}
所有默认文本值都在xml中,并设置为“0”
这是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)
答案 0 :(得分:1)
好的,首先让我们了解Activity的生命周期。在此link上,您可以查看更多详细信息。但就目前而言:
法。推出 - > onCreate - > onStart - > onResume - >运行...
提示:始终在onCreate方法中为活动设置View的元素(或者在onCreateView方法中使用片段的情况下。
请记住上面的生命周期,并查看“onCreate”方法。你能意识到你从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"));
您在此处设置无引用元素的文本。