我是java新手。
我试图在屏幕旋转时将变量从 OnSaveInstanceState
保存到 OnCreate
。
我已将保存到 savedInstanceState
Bundle 中的值记录下来。
当屏幕旋转时,OnCreate(Bundle savingInstanceState)
中的 Bundle 值在日志中显示为 0,但在日志中显示为 OnSaveInstanceState
的正确值。
我的 Activity Java 是
package com.hfad.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class StopwatchActivity extends Activity {
private static final String TAG = StopwatchActivity.class.getSimpleName();
private int seconds;
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if(savedInstanceState != null){
Log.d(TAG, "onCreate() Restoring previous state");
/* restore state */
seconds = savedInstanceState.getInt("seconds");
running = savedInstanceState.getBoolean("running");
String tmpStr = String.valueOf(seconds);
Log.d(TAG,tmpStr);
Log.d(TAG, "onCreate() ending");
} else {
Log.d(TAG, "onCreate() No saved state available");
/* initialize app */
}
runTimer();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.d(TAG,"onSaveInstanceState() saving state");
savedInstanceState.putInt("Seconds", seconds);
savedInstanceState.putBoolean("running", running);
String tmpStr = String.valueOf(savedInstanceState.getInt("Seconds"));
Log.d(TAG,tmpStr);
Log.d(TAG,"onSaveInstanceState() ending");
}
//Start the stopwatch running when the start button is clicked
public void onClickStart(View view){
running = true;
}
//Stop the stopwatch running when the stop button is clicked
public void onClickStop(View view){
running = false;
}
//Start the stopwatch running when the start button is clicked
public void onClickReset(View view){
running = false;
seconds = 0;
}
private void runTimer(){
final TextView timeView = (TextView)findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable(){
@Override
public void run(){
int hours = seconds/3600;
int minutes = (seconds%3600)/60;
int secs = seconds%60;
String time = String.format("%d:%02d:%02d",hours,minutes,secs);
timeView.setText(time);
if(running){
seconds++;
}
handler.postDelayed(this, 1000);
}
});
}
}
这是日志。
07-26 21:02:19.880 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onSaveInstanceState() saving state
07-26 21:02:19.880 6124-6124/com.hfad.stopwatch D/StopwatchActivity: 11
07-26 21:02:19.880 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onSaveInstanceState() ending
07-26 21:02:19.940 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onCreate() Restoring previous state
07-26 21:02:19.940 6124-6124/com.hfad.stopwatch D/StopwatchActivity: 0
07-26 21:02:19.940 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onCreate() ending
我正在学习书中的类(class),但这并不像所解释的那样起作用,而且我无法在网上找到解决方案。
最佳答案
您使用了错误的键来查找值。您的 key 中有一个大写的 S
。 Bundle
中的键区分大小写。
这里的一个好习惯是将键字符串声明为最终静态并使用它们来存储和检索。例如
public static final String SECONDS_KEY = "seconds";
public static final String RUNNING_KEY = "running"
关于java - bundle 失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38598952/