简单的Android活动导致崩溃

时间:2013-05-07 01:16:58

标签: java android android-activity

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

以下是我使用按钮进入新活动的活动:

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);
        }
    });

}

}

当我尝试启动时,这是我的活动崩溃(是的,这是我在找到问题之前输入的所有内容)

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)

3 个答案:

答案 0 :(得分:1)

在日志中:

  

.ActivityNotFoundException:无法找到显式活动类   {brian.android.testgame / brian.android.testgame.Level1Activity};

表示您忘记在Level1Activity注册AndroidManifest.xml活动。所以在AndroidManifest中声明它:

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

答案 1 :(得分:1)

在androidmanifest.xml中添加活动

<activity
    android:name=".Level1Activity" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

答案 2 :(得分:0)

您需要在manifest.xml文件中声明您的活动:

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

一个很好的提示是,无论何时创建新活动,都要立即在清单文件中声明它以避免进一步的错误。 :d

干杯,