-1
public class CheatActivity extends AppCompatActivity {


private static final String EXTRA_ANSWER_IS_TRUE="com.example.ferhat.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN="com.example.ferhat.geoquiz.answer_shown";
private static final String CHEATER="com.example.ferhat.geoquiz.cheated";
private Boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private Boolean mIsCheater;
public static Intent newIntent(Context packageContext, boolean answerIsTrue){
    Intent i=new Intent(packageContext,CheatActivity.class);
    i.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
    return i;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);

    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
    mAnswerTextView = (TextView) findViewById(R.id.answerTextView);

    mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {

        //Cevabı gösteriyor ve Kopya çekildi bilgisi veriliyor
        @Override
        public void onClick(View v) {
            if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button);
            } else {
                mAnswerTextView.setText(R.string.false_button);
            }
            mIsCheater=true;
            setAnswerShownResult();
        }
    });

     if(savedInstanceState!=null){
        mIsCheater=savedInstanceState.getBoolean(CHEATER,false);
    }

}

    private void setAnswerShownResult(){
    Intent data=new Intent();
    data.putExtra(EXTRA_ANSWER_SHOWN,mIsCheater);
    setResult(RESULT_OK,data);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean(CHEATER,mIsCheater);
}
}

I try to solve challenge in Anroid Programming, Big Nerds Ranch Guide (Chap.5) Challenge asks me to keep Cheat data while rotation of screen and transaction between questions.Main Activity holds questions and CheatActivity has answers from Main activity. And i created BooleanArray to hold cheat data for questions.

Problem is ,i cheated for first question and then when i am in the CheatActivity(CheatPage) of other questions ,program crashes if i rotate the screen.

Error caused by this line savedInstanceState.putBoolean(CHEATER,mIsCheater); i think i need to clear data from previous Cheat Data(BooleanArray already holding it) but i dont know how to do it.

Ferhat Ergün
  • 135
  • 1
  • 10
  • can you please post your log? – Abdul Waheed Mar 10 '17 at 08:18
  • What is 'CHEATER'? Would need to know 'type' and 'value' to help you. Also provide the stacktrace. – Preston Garno Mar 10 '17 at 08:22
  • [link](https://imgur.com/a/zoOFA) – Ferhat Ergün Mar 10 '17 at 08:47
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Christopher Mar 10 '17 at 08:47
  • Crash at line where `putBoolean` is does not make any sense. Either crash is not really there, or you are calling `onSaveInstanceState` manually with `null` bundle. – Yaroslav Mytkalyk Mar 10 '17 at 09:05
  • When i am getting first answer and rotating screen in that page wont cause any crash and keeps the data(that is what i want), But after first cheat, if i rotate screen in other CheatPages it crashes. If i remove onSaveInstanceState and SavedInstanceState , i cant keep data when i rotate screen. – Ferhat Ergün Mar 10 '17 at 09:20
  • I changed like this `private Boolean mIsCheater=false;` , now i am not getting crashes but i cant keep data when i am in the CheatPage while rotating screen – Ferhat Ergün Mar 10 '17 at 09:34

2 Answers2

0
public class CheatActivity extends AppCompatActivity {


private static final String EXTRA_ANSWER_IS_TRUE = "com.example.ferhat.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.example.ferhat.geoquiz.answer_shown";
public static final String EXTRA_CHEATED = "com.example.ferhat.geoquiz.cheated";
private static final String CHEATER = "com.example.ferhat.geoquiz.cheated";
private Boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private Boolean mAnswerEverShown;
private Boolean twoStep=false;


//Yeni intent methodu yarattık Cevabı alıyor ve bu activity i başlatıyor
public static Intent newIntent(Context packageContext, boolean answerIsTrue, boolean checked) {
    Intent i = new Intent(packageContext, CheatActivity.class);
    i.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    i.putExtra(EXTRA_CHEATED, checked);
    return i;
}



private void setAnswerShownResult(Boolean isAnswerShown) {
    Intent data = new Intent();
    **if(mAnswerEverShown) {
        isAnswerShown=mAnswerEverShown;
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }else {
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }
    twoStep=isAnswerShown;**

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);

    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
    mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
    **mAnswerEverShown = getIntent().getBooleanExtra(EXTRA_CHEATED, false);**
    mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {

        //Cevabı gösteriyor ve Kopya çekildi bilgisi veriliyor
        @Override
        public void onClick(View v) {
            if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.true_button);
            } else {
                mAnswerTextView.setText(R.string.false_button);
            }
            twoStep=true;
            setAnswerShownResult(twoStep);
        }

    });

    **if (savedInstanceState != null) {
        setAnswerShownResult(savedInstanceState.getBoolean(CHEATER, false));
    }
}
    @Override
    public void onSaveInstanceState (Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putBoolean(CHEATER, twoStep);
    }
}**

i found solution like for every situation. first think i did; i get info from main activity

mAnswerEverShown = getIntent().getBooleanExtra(EXTRA_CHEATED, false);

And Then i changed setAnswerShownResult for two situation.If it is not cheated ever program sends current data(cheated or not). i marked where i changed with *.

Ferhat Ergün
  • 135
  • 1
  • 10
-1

You need to putBoolean before callback the super method.

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
  savedInstanceState.putBoolean(CHEATER,mIsCheater);
  super.onSaveInstanceState(savedInstanceState);
}

if not the CHEATER won't be saved and you can't call it when resume activity

Phạm Lam
  • 378
  • 1
  • 3
  • 14
  • it didnt work, i think my problem caused by previous data – Ferhat Ergün Mar 10 '17 at 09:15
  • what is your link 75? And have you ever clicked the button `mShowAnswer` before rotating? If not, the variable `mIsCheater` has not been initialized. Your code risk the crash because your variable `mIsCheater` is not initialized. The second, as i told above, even if its not the problem here but you always need to `putBoolean` before calling back the super method – Phạm Lam Mar 10 '17 at 09:28
  • as i told `savedInstanceState.putBoolean(CHEATER,mIsCheater)` is link75 – Ferhat Ergün Mar 10 '17 at 09:32
  • so as i said the mIsCheater is not initialized. You should declare it by default `false` (in this case i think) – Phạm Lam Mar 10 '17 at 09:38