![](/img/trans.png)
[英]Where is Bundle object created in onCreate(Bundle savedInstanceState)
[英]How do I used the Bundle savedInstanceState in the onCreate method?
我试图将变量mCurrentIndex保存到savedInstanceState包中,以便在我旋转屏幕时我的应用程序不会重新启动。 我该如何将这个变量放到捆绑包中? 每次我尝试它时,我都会继续获得一个空对象引用。 这是我正在使用的当前代码:
基本上,我试图使用onSaveInstanceState方法来存储mCurrentIndex的值,然后在onCreate方法中我想要检索这个值。 如果我将savedInstanceState.putInt()放在onCreate方法的任何地方,我会得到一个空对象引用。
package com.example.geoquiz;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};
public int mCurrentIndex = 0;
// ...
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { checkAnswer(true); }
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { checkAnswer(false); }
});
mNextButton = (Button) findViewById(R.id.next_button);
//savedInstanceState.putInt("index", mCurrentIndex);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
//mCurrentIndex = currentIndex;
updateQuestion();
}
});
//savedInstanceState.putInt("index",mCurrentIndex);
updateQuestion();
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("index",mCurrentIndex);
}
private void updateQuestion() {
//mCurrentIndex = savedInstanceState.getInt("index");
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
} // to close the entire class
为了在不获取空指针异常的情况下执行此操作,您可以:
if (savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt("index");
}
此外,onSavedInstanceState方法具有以下格式:
@Override
public void onSaveInstanceState(Bundle outState){
outState.putInt("index",mCurrentIndex);
super.onSaveInstanceState(outState);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.