gpt4 book ai didi

java - 简单的 Android Activity 导致崩溃

转载 作者:行者123 更新时间:2023-12-02 07:02:59 25 4
gpt4 key购买 nike

我开始为我的 Android 应用程序创建一个新的 Activity,当我测试它时,我遇到了一个问题。当我触摸按钮进入新 Activity 时,应用程序会崩溃并返回到主要 Activity 。

这是我的 Activity ,其中包含转到新 Activity 的按钮:

package brian.android.testgame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class BeginnerLevelSelector extends Activity {

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.beginner_level_selector);

Button level1button = (Button) findViewById(R.id.bl1);

final Intent level1activity = new Intent(this, Level1Activity.class);

level1button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
startActivity(level1activity);
}
});

}

}

这是我的 Activity ,当我尝试启动它时崩溃(是的,这就是我在发现问题之前输入的所有内容)

package brian.android.testgame;

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

public class Level1Activity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);

}

}

这是应用程序崩溃时我的日志:

05-06 21:07:29.085: E/AndroidRuntime(20391): FATAL EXCEPTION: main
05-06 21:07:29.085: E/AndroidRuntime(20391): android.content.ActivityNotFoundException: Unable to find explicit activity class {brian.android.testgame/brian.android.testgame.Level1Activity}; have you declared this activity in your AndroidManifest.xml?
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1634)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1510)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.app.Activity.startActivityForResult(Activity.java:3244)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.app.Activity.startActivity(Activity.java:3351)
05-06 21:07:29.085: E/AndroidRuntime(20391): at brian.android.testgame.BeginnerLevelSelector$1.onClick(BeginnerLevelSelector.java:22)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.view.View.performClick(View.java:3549)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.view.View$PerformClick.run(View.java:14400)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.os.Handler.handleCallback(Handler.java:605)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.os.Looper.loop(Looper.java:154)
05-06 21:07:29.085: E/AndroidRuntime(20391): at android.app.ActivityThread.main(ActivityThread.java:4944)
05-06 21:07:29.085: E/AndroidRuntime(20391): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 21:07:29.085: E/AndroidRuntime(20391): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 21:07:29.085: E/AndroidRuntime(20391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-06 21:07:29.085: E/AndroidRuntime(20391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-06 21:07:29.085: E/AndroidRuntime(20391): at dalvik.system.NativeStart.main(Native Method)

最佳答案

如日志中所示:

.ActivityNotFoundException: Unable to find explicit activity class {brian.android.testgame/brian.android.testgame.Level1Activity};

表示您忘记在AndroidManifest.xml中注册Level1Activity Activity。所以将其声明为 AndroidManifest :

<activity android:name=".Level1Activity" />

关于java - 简单的 Android Activity 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com