Solution for "Unfortunately, Geoquiz has stopped "


#1

First of all, sorry for bad English, cuz I am not a native speaker.
If you get the following error in logcat, while you run your first apps in emulator

04-13 00:19:48.785: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.access$800(ActivityThread.java:135) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Handler.dispatchMessage(Handler.java:102) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Looper.loop(Looper.java:136) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invokeNative(Native Method) 04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invoke(Method.java:515) 04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-13 00:19:48.785: E/AndroidRuntime(1256): at dalvik.system.NativeStart.main(Native Method) 04-13 00:19:48.785: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException 04-13 00:19:48.785: E/AndroidRuntime(1256): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Activity.performCreate(Activity.java:5231) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 04-13 00:19:48.785: E/AndroidRuntime(1256): ... 11 more

Simply, the meassage shows you that you get a nullPointerException at .QuizActivity.OnCreate method.
And the problem is that both findViewById(R.id.true_button); findViewById(R.id.false_button); return null back to the mTrueButton and mFalseButton.

Most likely, it is caused by typo in activity_quiz.xml, as you cannot type the two ids correctly for the two buttons.(Noticed that there is a “+” sign in between “@” and “id”) . I give my code for your reference.

[code] <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" />[/code]

Further checking:
[ul][li]In Graphical layout of activity_quiz.xml,click on the true and false button, and make sure that in properties tab, the id is shown properly.[/li]
[li]In R.java file, check if the value of true_button and false_button are properly generated.[/li]
[li]Adding the following code, monitor the logcat to see whether mTrueButton and mFalseButton is null.

[code]mTrueButton = (Button) findViewById(R.id.true_button);
mFalseButton = (Button) findViewById(R.id.false_button);

    System.out.println("mTrueButtonIsNull: "+(mTrueButton==null));
    System.out.println("mFalseButtonIsNull: "+(mFalseButton==null));
    mTrueButton.setOnClickListener(new View.OnClickListener(){[/code] [/li][/ul]

:laughing: ,


#2

Use Blank Activity and try again.