Force close when starting new activity
I'm trying to launch a new activity from my main activity, but I just get error codes all the time.
Heres my main activity;
package com.gunstats;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class gunstats extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.deagle);
Button button3 = (Button)this.findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.ak47);
Button button2 = (Button)this.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp2.start();
}
});
final MediaPlayer mp3 = MediaPla开发者_如何学Cyer.create(this, R.raw.m40a3);
Button button1 = (Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp3.start();
}
});
Button button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(gunstats.this,
more.class);
startActivity(intent);
}
});
}
}
and the activity that is being called from my main class;
package com.gunstats;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class more extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.deagle);
Button buttonm1 = (Button)this.findViewById(R.id.buttonm1);
buttonm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gunstats"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".gunstats"
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=".more"/>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Logcat:
01-08 17:37:47.658: ERROR/AndroidRuntime(276): Uncaught handler: thread main exiting due to uncaught exception
01-08 17:37:47.728: ERROR/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gunstats/com.gunstats.more}: java.lang.NullPointerException
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4203)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): Caused by: java.lang.NullPointerException
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at com.gunstats.more.onCreate(more.java:23)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
01-08 17:37:47.728: ERROR/AndroidRuntime(276): ... 11 more
The error is pointing to my 23 line in the 'more' class.
my 23 line is; buttonm1.setOnClickListener(new View.OnClickListener() {
Whats wrong?
And there's nothing wrong in the manifest
I think there probably is. This error line:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gunstats/com.gunstats.more}; have you declared this activity in your AndroidManifest.xml? 01-08
Suggests you haven't defined the Activity
"more" in the manifest. Check that it is defined.
Also, Java convention is that class names have the first letter capitalised. So the class should be called More
.
You should add "more" Activity to the AndroidManifest.xml
精彩评论