This is for the last challenge of Chapter 2: MVC
LogCat shows this error whenever I run the app. It crashes immediately.
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to android.widget.ImageButton
Couldn’t figure out how to condense code so I had to copy and paste directly.
QUIZACTIVITY:
package com.bignerdranch.andoid.geoquiz;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import static com.bignerdranch.andoid.geoquiz.R.id.question_text_view;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mPrevButton;
private ImageButton mNextButton;
private TextView mQuestionTextView;
//Store question list into Question array to access later.
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),
new Question(R.string.question_canada, true),
};
//Keep count of questions in the Question array.
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
//Listener for TextView object. Every time it is pressed, the next question is displayed.
mQuestionTextView = (TextView) findViewById(question_text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
//Listener for true button. Displays correct toast if answer is right.
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
//Listener for false button. Displays incorrect toast if answer is wrong.
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
//Listener for prev button. Displays the previous question stored in Question array when Prev Button is pressed.
mPrevButton = (ImageButton) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mCurrentIndex == 0) {
mCurrentIndex = mCurrentIndex + (mQuestionBank.length - 1);
}
else {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
}
updateQuestion();
}
});
//Listener for next button. Displays next question stored in Question array when Next Button is pressed.
mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
updateQuestion();
}
//Updates questions.
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
//Checks if answer to the questions are right or wrong.
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();
}
}
quiz_acitivity.xml:
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_left"
android:contentDescription="@null" />
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_right"
android:contentDescription="@null" />
</LinearLayout>
Logtcat:
2020-01-08 16:46:58.054 3974-3974/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bignerdranch.andoid.geoquiz, PID: 3974
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.andoid.geoquiz/com.bignerdranch.andoid.geoquiz.QuizActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to android.widget.ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to android.widget.ImageButton
at com.bignerdranch.andoid.geoquiz.QuizActivity.onCreate(QuizActivity.java:69)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)