App crashes after implementing challenge code


#1

Hi all,

Here are the lines I added to the CheatActivity class code for the challenge:

private TextView mApiView;

protected void onCreate(Bundle savedInstanceState) {
.
.
.
mApiView = (TextView)findViewById(R.id.showAPI);
mApiView.setText(“API level” + Build.VERSION.SDK_INT);

But now when I press the Cheat button, I get the Unfortunately GeoQuiz has stopped working message. Please help.


#2

Hi,

Could you include more of the code from your CheatActivity.java file along with the Logcat error you’re seeing? It’s a little hard to see what the problem is.


#3

Here is the code from CheatActivity:

[code]package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity {

private TextView mApiView;
private TextView mAnswerTextView;
private Button mShowAnswer;

private boolean isAnswerShown;

public static final String CHEAT_INDEX = "com.bignerdranch.android.geoquiz.cheat_index";
public static final String EXTRA_IS_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown";
public static final String EXTRA_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";

private boolean mAnswerIsTrue;

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

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
	super.onSaveInstanceState(savedInstanceState);
	savedInstanceState.putBoolean(CHEAT_INDEX, isAnswerShown);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
    mApiView = (TextView)findViewById(R.id.showAPI);
	mApiView.setText("API level" + Build.VERSION.SDK_INT);
	
	if(savedInstanceState!=null){
		isAnswerShown=savedInstanceState.getBoolean(CHEAT_INDEX);
	}
	
	setAnswerShownResult();
	setContentView(R.layout.activity_cheat);
	
	mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_IS_TRUE, false);
	
	mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
	
	mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
	mShowAnswer.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(mAnswerIsTrue)
				mAnswerTextView.setText(R.string.true_button);
			else
				mAnswerTextView.setText(R.string.false_button);
			isAnswerShown=true;
			setAnswerShownResult();
		}
	});
}

}
[/code]

Here is the logcat:

08-20 17:41:09.231: D/QuizActivity(1090): onCreate(Bundle) called
08-20 17:41:09.261: D/QuizActivity(1090): onStart() called
08-20 17:41:09.261: D/QuizActivity(1090): onResume() called
08-20 17:41:09.271: D/QuizActivity(1090): onPause() called
08-20 17:41:09.301: I/Adreno-EGL(1090): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
08-20 17:41:09.301: I/Adreno-EGL(1090): OpenGL ES Shader Compiler Version: E031.24.00.07
08-20 17:41:09.301: I/Adreno-EGL(1090): Build Date: 04/07/14 Mon
08-20 17:41:09.301: I/Adreno-EGL(1090): Local Branch: au011
08-20 17:41:09.301: I/Adreno-EGL(1090): Remote Branch:
08-20 17:41:09.301: I/Adreno-EGL(1090): Local Patches:
08-20 17:41:09.301: I/Adreno-EGL(1090): Reconstruct Branch:
08-20 17:41:09.321: D/OpenGLRenderer(1090): Enabling debug mode 0
08-20 17:41:09.331: I/QuizActivity(1090): onSaveInstanceState
08-20 17:41:09.331: D/QuizActivity(1090): onStop() called
08-20 17:41:09.371: W/IInputConnectionWrapper(1090): showStatusIcon on inactive InputConnection
08-20 17:41:09.381: I/ActivityManager(1090): Timeline: Activity_idle id: android.os.BinderProxy@43686138 time:91595091
08-20 17:41:10.851: D/QuizActivity(1090): onStart() called
08-20 17:41:11.141: D/QuizActivity(1090): onResume() called
08-20 17:41:11.141: I/ActivityManager(1090): Timeline: Activity_idle id: android.os.BinderProxy@43686138 time:91596849
08-20 17:41:12.651: I/ActivityManager(1090): Timeline: Activity_launch_request id:com.bignerdranch.android.geoquiz time:91598366
08-20 17:41:12.661: D/QuizActivity(1090): onPause() called
08-20 17:41:12.671: D/AndroidRuntime(1090): Shutting down VM
08-20 17:41:12.671: W/dalvikvm(1090): threadid=1: thread exiting with uncaught exception (group=0x415acd88)
08-20 17:41:12.681: E/AndroidRuntime(1090): FATAL EXCEPTION: main
08-20 17:41:12.681: E/AndroidRuntime(1090): Process: com.bignerdranch.android.geoquiz, PID: 1090
08-20 17:41:12.681: E/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.CheatActivity}: java.lang.NullPointerException
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.os.Handler.dispatchMessage(Handler.java:102)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.os.Looper.loop(Looper.java:212)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.ActivityThread.main(ActivityThread.java:5135)
08-20 17:41:12.681: E/AndroidRuntime(1090): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 17:41:12.681: E/AndroidRuntime(1090): at java.lang.reflect.Method.invoke(Method.java:515)
08-20 17:41:12.681: E/AndroidRuntime(1090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
08-20 17:41:12.681: E/AndroidRuntime(1090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
08-20 17:41:12.681: E/AndroidRuntime(1090): at dalvik.system.NativeStart.main(Native Method)
08-20 17:41:12.681: E/AndroidRuntime(1090): Caused by: java.lang.NullPointerException
08-20 17:41:12.681: E/AndroidRuntime(1090): at com.bignerdranch.android.geoquiz.CheatActivity.onCreate(CheatActivity.java:44)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.Activity.performCreate(Activity.java:5231)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-20 17:41:12.681: E/AndroidRuntime(1090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
08-20 17:41:12.681: E/AndroidRuntime(1090): … 11 more
08-20 17:41:14.191: I/Process(1090): Sending signal. PID: 1090 SIG: 9


#4

This is the line that’s throwing the JavaNullPointerException:

Looks like this widget is coming back null from your xml layout file for some reason:

Double check your layout file and/or post it here.


#5

[quote=“rcubed”]This is the line that’s throwing the JavaNullPointerException:

Looks like this widget is coming back null from your xml layout file for some reason:

Double check your layout file and/or post it here.[/quote]

Hi, thanks for your reply. Here is my xml file for CheatActivity:

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

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/warning_text"
    />

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

<Button 
    android:id="@+id/showAnswerButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/show_answer_button"
    />

<TextView
    android:id="@+id/showAPI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="15sp"
    android:textStyle="italic" />

#6

Scratching my head on this one. Assuming your source file matches the .apk file that threw the exception (at line 44), then the TextView ref is null. Surprised it compiles OK. You may want to try running the app in the debugger and setting a breakpoint on the first line of the onCreate method. Then step through and check the value that the TextView is being set to. If it truly is coming back as null, then that would be pretty weird.


#7

Doh! After looking at your code in onCreate, you need to move the following line up prior to inflating the TextView widget:


#8

[quote=“rcubed”]Doh! After looking at your code in onCreate, you need to move the following line up prior to inflating the TextView widget:

Damn! Should have thought of that. Thanks a lot for your help :slight_smile:

Edit: Tested, works great, thanks again.


#9

Was having much the same problem (similar errors) as yours, but my issue was that I forgot to add the TextView to the landscape xml as well as the original vertical. My tablet for testing was horizontal when I was trying to run it, so I couldn’t figure out why the app kept crashing on launch. Posting here for any fellow idiots :sunglasses:


#10

Except that we never created a landscape view for the cheat activity only the quiz activity. The instructions here were to include the API version on the cheat screen (you can see it in the pic they provide). As we did not need to create a custom landscape view for this there should be no issue when you rotate. I’d guess either you decided to create a landscape view for this anyway or you incorrectly put it on the quiz view.