This is really frustrating me and none of the other questions solved my problem.
I'm using a test app from a book, and copied all the code verbatim. Everything was running fine earlier, but when I tried to make my own style, it started crashing. So I undid everything and deleted the XML file for the style, and it's still crashing.
What happens is I click on the app, the background for the app appears, but none of the buttons I added come up; instead, it says "Unfortunately, (appname) has stopped."
The console isn't giving me any problems, but here's what LogCat says:
12-29 20:45:29.346: D/AndroidRuntime(2606): Shutting down VM
12-29 20:45:29.346: W/dalvikvm(2606): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-29 20:45:29.394: E/AndroidRuntime(2606): FATAL EXCEPTION: main
12-29 20:45:29.394: E/AndroidRuntime(2606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.recipesapp/com.recipesapp.MainMenu}: java.lang.NullPointerException
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.os.Looper.loop(Looper.java:137)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-29 20:45:29.394: E/AndroidRuntime(2606): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 20:45:29.394: E/AndroidRuntime(2606): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 20:45:29.394: E/AndroidRuntime(2606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-29 20:45:29.394: E/AndroidRuntime(2606): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-29 20:45:29.394: E/AndroidRuntime(2606): at dalvik.system.NativeStart.main(Native Method)
12-29 20:45:29.394: E/AndroidRuntime(2606): Caused by: java.lang.NullPointerException
12-29 20:45:29.394: E/AndroidRuntime(2606): at com.recipesapp.MainMenu.onCreate(MainMenu.java:16)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.Activity.performCreate(Activity.java:4465)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-29 20:45:29.394: E/AndroidRuntime(2606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-29 20:45:29.394: E/AndroidRuntime(2606): ... 11 more
12-29 20:45:35.215: I/Process(2606): Sending signal. PID: 2606 SIG: 9
The only one I recognized was the java.lang.NullPointerException at com.recipesapp.MainMenu.onCreate(MainMenu.java:16)
so I zeroed in on that. And it turns out that when I comment out line 16 there, the app runs fine; however, I can't figure out why it's causing a problem.
Here is the error-generating code, with line 16 noted.
public class MainMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this); //LINE 16
}
public void onClick(View thisView) {
switch (thisView.getId()) {
case R.id.main_about_button:
Intent showAbout = new Intent(this, About.class);
startActivity(showAbout);
break;
}
}
}
And here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.recipesapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme = "@android:style/Theme.Light" >
<activity
android:name=".MainMenu"
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=".About"
android:theme="@android:style/Theme.Dialog">
</activity>
</application>
</manifest>
And the code in layout/main.xml for the button in question:
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/exit_button"
android:layout_alignLeft="@+id/exit_button"
android:text="@string/main_about_button" />
The problem is you're mixing up the name of the Button.
Java:
View aboutButton = findViewById(R.id.main_about_button);
XML:
android:id="@+id/about_button"
Either change the second to @+id/main_about_button
or the first to R.id.about_button
and you should be good to go.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.