簡體   English   中英

我如何在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM