GeoQuiz stops when clicking Show Answer


#1

Upon clicking the Show Answer button on Cheat Activity, I get the following error: Unfortunately, GeoQuiz has stopped. If possible could, you please let me know if you see the what is wrong as well as how you came about figuring it out so I can become better at debugging. Thank you.

Below is the LogCat file:

09-01 19:43:03.354: D/QuizActivity(732): onPause() called
09-01 19:43:03.765: D/dalvikvm(732): GC_CONCURRENT freed 77K, 7% free 2737K/2932K, paused 6ms+28ms, total 242ms
09-01 19:43:03.875: W/Resources(732): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f080000}
09-01 19:43:04.288: W/EGL_emulation(732): eglSurfaceAttrib not implemented
09-01 19:43:05.124: I/QuizActivity(732): onSaveInstanceState
09-01 19:43:05.124: D/QuizActivity(732): onStop() called
09-01 19:43:09.885: D/CheatActivity(732): Show answer button clicked
09-01 19:43:09.885: D/CheatActivity(732): mAnswerIsTrue: true
09-01 19:43:09.896: D/CheatActivity(732): If statement worked
09-01 19:43:09.896: D/CheatActivity(732): mAnswerTextView: null
09-01 19:43:09.896: D/AndroidRuntime(732): Shutting down VM
09-01 19:43:09.905: W/dalvikvm(732): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-01 19:43:09.935: E/AndroidRuntime(732): FATAL EXCEPTION: main
09-01 19:43:09.935: E/AndroidRuntime(732): java.lang.NullPointerException
09-01 19:43:09.935: E/AndroidRuntime(732): at com.kb.android.geoquiz.CheatActivity$1.onClick(CheatActivity.java:56)
09-01 19:43:09.935: E/AndroidRuntime(732): at android.view.View.performClick(View.java:4204)
09-01 19:43:09.935: E/AndroidRuntime(732): at android.view.View$PerformClick.run(View.java:17355)
09-01 19:43:09.935: E/AndroidRuntime(732): at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:43:09.935: E/AndroidRuntime(732): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:43:09.935: E/AndroidRuntime(732): at android.os.Looper.loop(Looper.java:137)
09-01 19:43:09.935: E/AndroidRuntime(732): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:43:09.935: E/AndroidRuntime(732): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:43:09.935: E/AndroidRuntime(732): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:43:09.935: E/AndroidRuntime(732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:43:09.935: E/AndroidRuntime(732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:43:09.935: E/AndroidRuntime(732): at dalvik.system.NativeStart.main(Native Method)
09-01 19:43:30.855: E/Trace(751): error opening trace file: No such file or directory (2)
09-01 19:43:31.654: D/QuizActivity(751): onStart() called
09-01 19:43:31.664: D/QuizActivity(751): onResume() called
09-01 19:43:31.916: D/libEGL(751): loaded /system/lib/egl/libEGL_emulation.so
09-01 19:43:31.934: D/(751): HostConnection::get() New Host Connection established 0x2a154d08, tid 751
09-01 19:43:31.934: D/libEGL(751): loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-01 19:43:31.954: D/libEGL(751): loaded /system/lib/egl/libGLESv2_emulation.so
09-01 19:43:32.055: W/EGL_emulation(751): eglSurfaceAttrib not implemented
09-01 19:43:32.064: D/OpenGLRenderer(751): Enabling debug mode 0
09-01 19:43:50.344: D/dalvikvm(751): GC_CONCURRENT freed 77K, 7% free 2772K/2968K, paused 9ms+5ms, total 144ms
09-01 19:43:51.714: D/QuizActivity(751): onPause() called
09-01 19:43:52.004: W/Resources(751): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f080000}
09-01 19:43:52.374: W/EGL_emulation(751): eglSurfaceAttrib not implemented
09-01 19:43:53.044: I/QuizActivity(751): onSaveInstanceState
09-01 19:43:53.044: D/QuizActivity(751): onStop() called
09-01 19:43:53.954: D/CheatActivity(751): Show answer button clicked
09-01 19:43:53.954: D/CheatActivity(751): mAnswerIsTrue: false
09-01 19:43:53.964: D/AndroidRuntime(751): Shutting down VM
09-01 19:43:53.964: W/dalvikvm(751): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-01 19:43:53.994: E/AndroidRuntime(751): FATAL EXCEPTION: main
09-01 19:43:53.994: E/AndroidRuntime(751): java.lang.NullPointerException
09-01 19:43:53.994: E/AndroidRuntime(751): at com.kb.android.geoquiz.CheatActivity$1.onClick(CheatActivity.java:60)
09-01 19:43:53.994: E/AndroidRuntime(751): at android.view.View.performClick(View.java:4204)
09-01 19:43:53.994: E/AndroidRuntime(751): at android.view.View$PerformClick.run(View.java:17355)
09-01 19:43:53.994: E/AndroidRuntime(751): at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:43:53.994: E/AndroidRuntime(751): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:43:53.994: E/AndroidRuntime(751): at android.os.Looper.loop(Looper.java:137)
09-01 19:43:53.994: E/AndroidRuntime(751): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:43:53.994: E/AndroidRuntime(751): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:43:53.994: E/AndroidRuntime(751): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:43:53.994: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:43:53.994: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:43:53.994: E/AndroidRuntime(751): at dalvik.system.NativeStart.main(Native Method)

Here is CheatActivity.java

package com.kb.android.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CheatActivity extends Activity {

private static final String TAG = "CheatActivity";
public static final String EXTRA_ANSWER_IS_TRUE = "com.kb.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.kb.android.geoquiz.answer_is_shown";

private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;

private void setAnswerShownResult(boolean isAnswerShown) {
	Intent data = new Intent();
	data.putExtra(EXTRA_ANSWER_IS_TRUE, isAnswerShown);
	setResult(RESULT_OK, data);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_cheat);
	

	// Hide answer until the user clicks the button
	setAnswerShownResult(true);

	mAnswerIsTrue = getIntent()
			.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

	mAnswerTextView = (TextView) findViewById(R.id.answerTextView); 
	
	mShowAnswer = (Button) findViewById(R.id.show_answer_button);

	mShowAnswer.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {

			Log.d(TAG, "Show answer button clicked");
			Log.d(TAG, "mAnswerIsTrue: " + mAnswerIsTrue);
			if (mAnswerIsTrue == true) {
				Log.d(TAG, "If statement worked");
				Log.d(TAG, "mAnswerTextView: " + mAnswerTextView);
				//mAnswerTextView was null, determine initial value of R.id.answerTextView
				mAnswerTextView.setText(R.string.true_button);
				
				
			} else {
				mAnswerTextView.setText(R.string.false_button);
			}
			
						}
	});

}

}

Here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.kb.android.geoquiz.QuizActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:label="@string/app_name" 
        android:name=".CheatActivity">
        
    </activity>
</application>

#2

It looks like you’re getting a NullPointerException inside your onClick listener. (You can see this method call inside your stack trace.)

Usually, NullPointerExceptions happen when you call a method on an object reference that is null. In this case, that appears to be mAnswerTextView. Since there is a call to findViewById() to assign a value to mAnswerTextView, it is probably the case that answerTextView is not activity_cheat.xml.