-1

I started the Android language Studio and I only need one step to finish this first TD.

Here are the instructions from TD:

In MainActivity, create an array of Boolean values mQuestionTrichee, to browse with variable mIndexActuel

ok, so here that's I did:

private static final String A_TRICHE = "a triche";
private boolean[] mQuestionTrichee = new boolean[mTabQuestions.length];

After, I have the value of Question Cheat [mIndexActuel] in the Bundle in the onSaveInstanceState () method:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.i(TAG, "onSaveInstanceState");
    outState.putInt(KEY_INDEX, mIndexActuel);
    outState.putBoolean(A_TRICHE, mQuestionTrichee[mIndexActuel]);
}

So, then, I have to get its value in onCreate ():

   @Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate appelee");
    setContentView(R.layout.activity_main);
    mQuestionTrichee[mIndexActuel]=savedInstanceState.getBoolean(A_TRICHE);

 }

but I have this error later:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String)' on a null object reference

I tried to browse my table in the method onCreate () or onSaveInstanceState () but nothing changed I still have it.

Here's what I added:

Application

   if(savedInstanceState != null)
    {
        for(mIndexActuel=0; mIndexActuel < mTabQuestions.length; mIndexActuel++)
        {
            if(mQuestionTrichee[mIndexActuel]==true)
            {
                mQuestionTrichee[mIndexActuel] = true;
            }
            else
            {
                mQuestionTrichee[mIndexActuel] = false;
            }
        }
        mIndexActuel = savedInstanceState.getInt(KEY_INDEX,0);
        mQuestionTrichee[mIndexActuel] = savedInstanceState.getBoolean(A_TRICHE);
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

In your method public void onCreate(Bundle savedInstanceState) { ... } you need to do a null check:

public void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mQuestionTrichee[mIndexActuel] = savedInstanceState.getBoolean(A_TRICHE);
    }
}

The bundle savedInstanceState will be null if the state of the app has not previously been saved.

Androidas
  • 169
  • 10
  • Ok thank you very much for your help, already my application starts. I just have one last problem, when I click on help normally the following message is displayed "You are a cheater" but when I turn the tablet the user can answer again, here is my real problem .. I think that it could come from the loop because of 1 I do not know or the declared is 2 I do not know if it is correct or not .. – B.Rousseaux Feb 02 '18 at 14:57
0

Inside the onCreate(), the part where you're trying to get the boolean from savedInstanceState, you're getting a NullPointerException since in onCreate() sometimes the value of savedInstanceState returned is null. What you can do is either put an if condition before the line savedInstanceState.getBoolean(A_TRICHE) or override the onRestoreInstanceState(Bundle savedInstanceState) method and place that line there which is also a better practice.

Debdeep
  • 752
  • 10
  • 22
-1

You should try this -

if( savedInstanceState != null){
    mQuestionTrichee[mIndexActuel] = savedInstanceState.getBoolean(A_TRICHE);
}

You need to check if the savedInstanceState only then try to get the value.

theboringdeveloper
  • 1,429
  • 13
  • 17
  • Your answer is just copypaste of @androidas and also you haven't explained our answer.Between I haven't voted you down :) . – Soham Feb 02 '18 at 14:25