user2154477
user2154477

Reputation: 145

Simple Android Activity Causes Crash

I started creating a new activity for my android app, and as I was testing it I encountered a problem. As i touched the button to go to the new activity, the app would crash and send be back to the main activity.

Here is my activity with the button that goes to the new 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);
        }
    });

}

}

Here is my activity that crashes when I try to start it (yes, that is all I typed before I found the problem)

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

}

}

And here is my log when the app crashes:

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)

Upvotes: 0

Views: 353

Answers (3)

Long Dao
Long Dao

Reputation: 1371

You need to declare your activity in the manifest.xml file:

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

A good tip is that, whenever you create a new activity, declare it immediately in the manifest file to avoid further errors. :D

Cheers,

Upvotes: 0

buptcoder
buptcoder

Reputation: 2702

add the activity in your 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>

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

as in Log:

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

means you forget to register Level1Activity Activity in AndroidManifest.xml. so declare it as in AndroidManifest :

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

Upvotes: 1

Related Questions