GeoQuiz Keeps Stopping after debugging code has been added


#1

Hi there, I’ve followed the book accordingly and now I have an issue with loading GeoQuiz. It keeps crashing everytime I keep trying to open the app

Here are my files

layout/activity_quiz.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></TextView>


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

<ImageButton
    android:id="@+id/previous_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow_left"/>


<ImageButton
    android:id="@+id/next_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow_right"/>

</LinearLayout>

land/activity_quiz.xml

<TextView
    android:id="@+id/question_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:padding="24dp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    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>


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/next_button"
    android:layout_gravity="bottom|right"
    android:text="@string/next_button"
    android:drawableRight="@drawable/arrow_right"
    android:drawablePadding="4dp"/>

strings.xml

GeoQuiz Canberra is the capital of Australia The Pacific Ocean is larger than the Atlantic Ocean The Suez Canal connects the Red Sea and the Indian Ocean The source of the Nile River is in Egypt The Amazon River is the longest river in the Americas Lake Baikal is the world\'s oldest and deepest freshwater lake True False Next Previous Correct! Incorrect!

quizactivity.java

package com.bignerdranch.android.geoquiz;

import android.annotation.SuppressLint;
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.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private static final String KEY_INDEX_ANSWERED_QUESTIONS = "index";

private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private ImageButton mPreviousButton;
private TextView mQuestionTextView;


private Question[] mQuestionBank = new Question[]{
        new Question(R.string.question_australia, true),
        new Question(R.string.question_ocean, 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;

@SuppressLint("WrongViewCast")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_quiz);

    int[] questionsAnswered = new int[mQuestionBank.length];
    for (int i = 0; i < mQuestionBank.length; i++) {
        questionsAnswered[i] = mQuestionBank[i].getAnswered();
    }

    savedInstanceState.putIntArray(KEY_INDEX_ANSWERED_QUESTIONS, questionsAnswered);


    if (savedInstanceState != null) {

        int[] questionAnswered = savedInstanceState.getIntArray(KEY_INDEX_ANSWERED_QUESTIONS);
        for (int i = 0; i < questionAnswered.length; i++) {
            mQuestionBank[i].setAnswered(questionsAnswered[i]);
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
        }
    }



    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkAnswer(false);
        }
    });

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkAnswer(true);
        }
    });

    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
    mQuestionTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

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


    mPreviousButton = (ImageButton) findViewById(R.id.previous_button);
    mPreviousButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

    updateQuestion();
}

@Override
public void onStart () {
    super.onStart();
    Log.d(TAG, "onStart() called");
}

@Override
public void onResume () {
    super.onResume();
    Log.d(TAG, "onResume() called");
}

@Override
public void onPause () {
    super.onPause();
    Log.d(TAG, "onPause() called");
}

@Override
public void onSaveInstanceState (Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}

@Override
public void onStop () {
    super.onStop();
    Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy () {
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
}


private void updateQuestion() {
    //Log.d(TAG, "Updating question text", new Exception());
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);
}

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

}

}

class.class

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package java.lang;

import java.io.InputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.net.URL;
import java.security.ProtectionDomain;

public final class Class implements Serializable, GenericDeclaration, Type, AnnotatedElement {
Class() {
throw new RuntimeException(“Stub!”);
}

public String toString() {
    throw new RuntimeException("Stub!");
}

public String toGenericString() {
    throw new RuntimeException("Stub!");
}

public static Class<?> forName(String className) throws ClassNotFoundException {
    throw new RuntimeException("Stub!");
}

public static Class<?> forName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException {
    throw new RuntimeException("Stub!");
}

public native T newInstance() throws InstantiationException, IllegalAccessException;

public boolean isInstance(Object obj) {
    throw new RuntimeException("Stub!");
}

public boolean isAssignableFrom(Class<?> cls) {
    throw new RuntimeException("Stub!");
}

public boolean isInterface() {
    throw new RuntimeException("Stub!");
}

public boolean isArray() {
    throw new RuntimeException("Stub!");
}

public boolean isPrimitive() {
    throw new RuntimeException("Stub!");
}

public boolean isAnnotation() {
    throw new RuntimeException("Stub!");
}

public boolean isSynthetic() {
    throw new RuntimeException("Stub!");
}

public String getName() {
    throw new RuntimeException("Stub!");
}

public ClassLoader getClassLoader() {
    throw new RuntimeException("Stub!");
}

public synchronized TypeVariable<Class<T>>[] getTypeParameters() {
    throw new RuntimeException("Stub!");
}

public Class<? super T> getSuperclass() {
    throw new RuntimeException("Stub!");
}

public Type getGenericSuperclass() {
    throw new RuntimeException("Stub!");
}

public Package getPackage() {
    throw new RuntimeException("Stub!");
}

public Class<?>[] getInterfaces() {
    throw new RuntimeException("Stub!");
}

public Type[] getGenericInterfaces() {
    throw new RuntimeException("Stub!");
}

public Class<?> getComponentType() {
    throw new RuntimeException("Stub!");
}

public int getModifiers() {
    throw new RuntimeException("Stub!");
}

public Object[] getSigners() {
    throw new RuntimeException("Stub!");
}

public Method getEnclosingMethod() {
    throw new RuntimeException("Stub!");
}

public Constructor<?> getEnclosingConstructor() {
    throw new RuntimeException("Stub!");
}

public native Class<?> getDeclaringClass();

public native Class<?> getEnclosingClass();

public String getSimpleName() {
    throw new RuntimeException("Stub!");
}

public String getTypeName() {
    throw new RuntimeException("Stub!");
}

public String getCanonicalName() {
    throw new RuntimeException("Stub!");
}

public native boolean isAnonymousClass();

public boolean isLocalClass() {
    throw new RuntimeException("Stub!");
}

public boolean isMemberClass() {
    throw new RuntimeException("Stub!");
}

public Class<?>[] getClasses() {
    throw new RuntimeException("Stub!");
}

public Field[] getFields() throws SecurityException {
    throw new RuntimeException("Stub!");
}

public Method[] getMethods() throws SecurityException {
    throw new RuntimeException("Stub!");
}

public Constructor<?>[] getConstructors() throws SecurityException {
    throw new RuntimeException("Stub!");
}

public Field getField(String name) throws NoSuchFieldException {
    throw new RuntimeException("Stub!");
}

public Method getMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException {
    throw new RuntimeException("Stub!");
}

public Constructor<T> getConstructor(Class... parameterTypes) throws NoSuchMethodException, SecurityException {
    throw new RuntimeException("Stub!");
}

public native Class<?>[] getDeclaredClasses();

public native Field[] getDeclaredFields();

public Method[] getDeclaredMethods() throws SecurityException {
    throw new RuntimeException("Stub!");
}

public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
    throw new RuntimeException("Stub!");
}

public native Field getDeclaredField(String var1) throws NoSuchFieldException;

public Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException {
    throw new RuntimeException("Stub!");
}

public Constructor<T> getDeclaredConstructor(Class... parameterTypes) throws NoSuchMethodException, SecurityException {
    throw new RuntimeException("Stub!");
}

public InputStream getResourceAsStream(String name) {
    throw new RuntimeException("Stub!");
}

public URL getResource(String name) {
    throw new RuntimeException("Stub!");
}

public ProtectionDomain getProtectionDomain() {
    throw new RuntimeException("Stub!");
}

public boolean desiredAssertionStatus() {
    throw new RuntimeException("Stub!");
}

public boolean isEnum() {
    throw new RuntimeException("Stub!");
}

public T[] getEnumConstants() {
    throw new RuntimeException("Stub!");
}

public T cast(Object obj) {
    throw new RuntimeException("Stub!");
}

public <U> Class<? extends U> asSubclass(Class<U> clazz) {
    throw new RuntimeException("Stub!");
}

public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
    throw new RuntimeException("Stub!");
}

public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
    throw new RuntimeException("Stub!");
}

public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
    throw new RuntimeException("Stub!");
}

public Annotation[] getAnnotations() {
    throw new RuntimeException("Stub!");
}

public native <A extends Annotation> A getDeclaredAnnotation(Class<A> var1);

public native Annotation[] getDeclaredAnnotations();

}

I TRULY APPRECIATE ALL THE HELP


#2

Hey,
I checked the layout/activity_quiz.xml file and you had closed the “TextView” tag after the “LinearLayout” tag I saw.
Delete your tag and closing like this I show below. Maybe this time it will work.

<TextView
    android:id="@+id/question_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"/> **---> this changed.**

<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>   **-----> And I deleted the tag here.**

#3

Hi Iscodex,
I tried what you pointed out and it says I have mismatch closing tags, and the app still stops.


#4

if you get mismatch closing tags, You should check again the layout. Because your textview element has the LinearLayout element., It is not good.After doing this, you check what is the logcat output show for the error?