GeoQuiz Keeps Stopping


#1

I am on Chapter 2 of the book, and I just added the Next Button. When trying to run the program on the emulator, I get a messaging saying GeoQuiz keeps stopping. Gradle is running fine with no errors, but I see exceptions in the Logcat:

03-04 15:13:34.835 8407-8407/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.bignerdranch.android.quizactivity, PID: 8407
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.quizactivity/com.bignerdranch.android.quizactivity.QuizActivity}: android.view.InflateException: Binary XML file line #0: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                  Caused by: android.view.InflateException: Binary XML file line #0: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                                                  Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                     at com.bignerdranch.android.quizactivity.QuizActivity.onCreate(QuizActivity.java:31)
                                                     at android.app.Activity.performCreate(Activity.java:6679)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

QuizActivity.java

package com.bignerdranch.android.quizactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class QuizActivity extends AppCompatActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;

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

    private int mCurrentIndex = 0;

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

        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

        mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
            }
        });

        mNextButton = (Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        updateQuestion();
    }

    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getTextResID();
        mQuestionTextView.setText(question);
    }
}

activity_quiz.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:gravity = "center"
    android:orientation="vertical">

    <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"/>
        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_button" />
    </LinearLayout>
    </TextView>
</LinearLayout> 

strings.xml

<resources>
        <string name="app_name">GeoQuiz</string>
        <string name="question_australia">Canberra is the capital of Australia.</string>
        <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
        <string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
        <string name="question_africa">The source of the Nile River is in Egypt.</string>
        <string name="true_button">True</string>
        <string name="question_americas">The Amazon River is the longest river in the Americas.</string>
        <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
        <string name="false_button">False</string>
        <string name="next_button">Next</string>
        <string name="correct_toast">Correct!</string>
        <string name="incorrect_toast">Incorrect!</string>
</resources>

Question.java

package com.bignerdranch.android.quizactivity;

public class Question {
    private int mTextResID;
    private boolean mAnswerTrue;

    public Question(int textResID, boolean answerTrue) {
        mTextResID = textResID;
        mAnswerTrue = answerTrue;
    }

    public int getTextResID() {
        return mTextResID;
    }

    public void setTextResID(int textResID) {
        mTextResID = textResID;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }
}

Any suggestions would be a big help!


#2

According to your crash log, the LayoutInflater attempted to cast an AppCompatTextView to ViewGroup. This happened because your question_text_view in activity_quiz.xml contains a LinearLayout as a child view.

To fix this, move the closing </TextView> tag from the second-last line of activity_quiz.xml to just after the <TextView...> it closes.