My Intent from the first activity below, always returns the default activity specified in CheatActivity. Any idea what I did wrong. I tried the debugger and it didn’t recognise my breakpoint:
package com.bignerdranch.geoquiz;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static com.bignerdranch.geoquiz.R.*;
public class MainActivity extends AppCompatActivity {
//class constants:
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
// create widget variables
private TextView mQuestionBox;
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPrevButton;
private Button mCheatButton;
private Question[] mQuestionBank = new Question[]
{ new Question(string.question_oceans, true),
new Question(string.question_mideast, false),
new Question(string.question_africa, false),
new Question(string.question_americas, true),
new Question(string.question_asia, true), };
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
if (savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
}
// wire the widget variables to those in the layout file
mQuestionBox = findViewById(id.questionBox);
mTrueButton = findViewById(id.trueButton);
mFalseButton = findViewById(id.falseButton);
mNextButton = findViewById(id.nextButton);
mPrevButton = findViewById(id.prevButton);
mCheatButton = findViewById(id.cheat_button);
// we add listeners to the buttons.
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!(mCurrentIndex < 0)) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
}
});
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!(mCurrentIndex < 0)) {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
}
}
});
// we add a listener to the TextView as well so that is goes to the next question
mQuestionBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,cheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
cheatActivity.newIntent(MainActivity.this,answerIsTrue);
startActivity(i);
}
});
}
@Override public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX, mCurrentIndex); }
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionBox.setText(question);
}
private void checkAnswer (boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerIsTrue){
messageResId = string.correctAnswer;
} else {
messageResId = string.incorrectAnswer;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
}
And
package com.bignerdranch.geoquiz;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class cheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE = “com.bignerdranch.android.geoquiz.answer_is_true”;
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
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);
setTitle(R.string.Cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
mAnswerTextView = findViewById(R.id.answer_text_view);
mShowAnswer = findViewById(R.id.show_answer_button);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.trueButton);
} else {
mAnswerTextView.setText(R.string.falseButton);
}
}
});
}
}