java捆绑包丢失值
我是java新手。
我试图在屏幕旋转时将变量从OnSaveInstanceState
保存到OnCreate
。
我已将保存的值记录到savedInstanceState
包中。
当屏幕旋转时,OnCreate(Bundle savedInstanceState)
中的捆绑值在日志中显示为0,但在日志中显示为OnSaveInstanceState
的正确值
我的活动是Java
package com.hfad.stopwatch;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.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
我正在一本书中学习课程,但这并不像我解释的那样有效,我也无法在网上找到解决方案
# 1 楼答案
设置要保存的值后,应该调用
super.onSaveInstanceState(savedInstanceState)
你可以看到更多in the documentations
# 2 楼答案
Bundle区分大小写。你正在输入“秒”并检索“秒”。好的做法是将键定义为常量,这样就不会出现这样的错误
Bundle - is key case sensitive?
# 3 楼答案
您使用了错误的键来查找值。你的钥匙上有一个大写字母
S
。aBundle
中的键区分大小写这里的一个好做法是将密钥字符串声明为最终静态,并使用它们来存储和检索。例如