Cheat Button Error


#1

For some reason when I go to run my app on my Nexus 4, after clicking on the Cheat button I get the Unexpectedly Quit error.

Here is the Logcat…
08-22 14:24:17.815: W/dalvikvm(27204): threadid=1: thread exiting with uncaught exception (group=0x41840700)
08-22 14:24:17.815: E/AndroidRuntime(27204): FATAL EXCEPTION: main
08-22 14:24:17.815: E/AndroidRuntime(27204): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.bignerdranch.geoquiz/com.bignerdranch.android.geoquiz.CheatActivity}; have you declared this activity in your AndroidManifest.xml?
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.Activity.startActivityForResult(Activity.java:3390)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.Activity.startActivityForResult(Activity.java:3351)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.Activity.startActivity(Activity.java:3587)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.Activity.startActivity(Activity.java:3555)
08-22 14:24:17.815: E/AndroidRuntime(27204): at com.bignerdranch.geoquiz.QuizActivity$6.onClick(QuizActivity.java:119)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.view.View.performClick(View.java:4240)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.view.View$PerformClick.run(View.java:17721)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.os.Handler.handleCallback(Handler.java:730)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.os.Looper.loop(Looper.java:137)
08-22 14:24:17.815: E/AndroidRuntime(27204): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-22 14:24:17.815: E/AndroidRuntime(27204): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 14:24:17.815: E/AndroidRuntime(27204): at java.lang.reflect.Method.invoke(Method.java:525)
08-22 14:24:17.815: E/AndroidRuntime(27204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-22 14:24:17.815: E/AndroidRuntime(27204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 14:24:17.815: E/AndroidRuntime(27204): at dalvik.system.NativeStart.main(Native Method)

Also, eclipse keeps trying to chang my

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
}
to
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.[color=#FF4000]activity_list_item[/color]);
}

My AndriodManifest.xml also has this per the error I am getting.

HELP!


#2

1- What package is your CheatActivity class defined in?

2- Where in your AndroidManifest is your tag?


#3

CheatActivity.java:

package com.bignerdranch.android.geoquiz;

import android.R;
import android.app.Activity;
import android.os.Bundle;

public class CheatActivity extends Activity {

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

}

AndoidManifest:

<?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.bignerdranch.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:name=".CheatActivity"
        android:label="@string/app_name" />
</application>

#4

There’s your issue. Your class is defined in com.bignerdranch.android.geoquiz:

package com.bignerdranch.android.geoquiz;

But your AndroidManifest.xml is pointing at com.bignerdranch.geoquiz:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.geoquiz"
android:versionCode="1"
android:versionName="1.0" >

You can fix it by moving CheatActivity into the correct package.


#5

Ok - So now it goes to another screen but then still shuts down. I moved ‘CheatActivity.java’ into the com.bignerdrance.geoquiz package. Here is the log cat.

08-26 10:52:27.001: I/QuizActivity(30389): onSaveInstanceState
08-26 10:52:27.001: D/AndroidRuntime(30389): Shutting down VM
08-26 10:52:27.011: W/dalvikvm(30389): threadid=1: thread exiting with uncaught exception (group=0x4150a700)
08-26 10:52:27.011: E/AndroidRuntime(30389): FATAL EXCEPTION: main
08-26 10:52:27.011: E/AndroidRuntime(30389): java.lang.NullPointerException
08-26 10:52:27.011: E/AndroidRuntime(30389): at com.bignerdranch.geoquiz.QuizActivity.onSaveInstanceState(QuizActivity.java:130)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.Activity.performSaveInstanceState(Activity.java:1147)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1223)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3177)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3236)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.ActivityThread.access$900(ActivityThread.java:141)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.os.Looper.loop(Looper.java:137)
08-26 10:52:27.011: E/AndroidRuntime(30389): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-26 10:52:27.011: E/AndroidRuntime(30389): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 10:52:27.011: E/AndroidRuntime(30389): at java.lang.reflect.Method.invoke(Method.java:525)
08-26 10:52:27.011: E/AndroidRuntime(30389): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-26 10:52:27.011: E/AndroidRuntime(30389): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-26 10:52:27.011: E/AndroidRuntime(30389): at dalvik.system.NativeStart.main(Native Method)


#6

Also, should I leave

setContentView(R.layout.activity_list_item); or change it back to setContentView(R.layout.activity_cheat); like the book has it (which keeps bringing up an error)?


#7

Check out your log - the very first line after “NullPointerException” is telling you which line in which file your error is.