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:
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);
}