我正在尝试从我的主要活动中启动一项新活动,但我一直都会收到错误代码。
继承我的主要活动;
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 = MediaPlayer.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);
}
});
}
}
以及从我的主类调用的活动;
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();
}
});
}
}
清单:
<?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
错误指向“更多”类中的23行。
我的23行是; buttonm1.setOnClickListener(new View.OnClickListener(){怎么了?
答案 0 :(得分:2)
清单中没有错误
我想可能有。此错误行:
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
建议您尚未在清单中定义Activity
“more”。检查它是否已定义。
此外,Java约定是类名首字母大写。因此,该类应该被称为More
。
答案 1 :(得分:2)
您应该将“更多”活动添加到AndroidManifest.xml