Dakota
Dakota

Reputation: 17

Whenever I start a new Activity, my app crashes

I've begun learning how to program for Android using Google's tutorials, but I've had a huge problem.

I have a Button which when clicked should switch the user to another screen (Activity), but whenever I run the app, it crashes.

This is the code that should start the next Activity when the Button is clicked:

public void addListenerOnButton() {

    Button button = (Button) findViewById(R.id.BeginShoppingButton);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent beginShopping = new Intent(getApplicationContext(), BeginShoppingScreen.class);
            startActivity(beginShopping);

        }

    });
}

This method is called in the onCreate() method so that the listener is active.

This is the XML for the Main Activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="eddgroceryapp.cartcourse2.Activities.MainActivity">

<Button
    android:text="Begin Shopping"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/BeginShoppingButton"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="Browse Stores"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:layout_below="@+id/BeginShoppingButton"
    android:layout_alignParentStart="true"
    android:onClick="findAStore"/>

<Button
    android:text="Report Locations"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:id="@+id/ReportLocationsButton"
    android:layout_below="@+id/BeginShoppingButton"
    android:layout_alignParentStart="true"
    android:onClick="findAStore"/>
</RelativeLayout>

And this is the XML for the other Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:text="Choose From Saved Lists"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Choose" />

<Button
    android:text="Create A List"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Create" />

<Button
    android:text="Use Someone Else's List"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button4" />
</LinearLayout>

Crash Log:

11-01 13:51:28.166 1345-1345/eddgroceryapp.cartcourse2 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: eddgroceryapp.cartcourse2, PID: 1345
 android.content.ActivityNotFoundException: Unable to find explicit activity class {eddgroceryapp.cartcourse2/eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen}; have you declared this activity in your AndroidManifest.xml?
     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1855)
     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546)
     at android.app.Activity.startActivityForResult(Activity.java:4284)
     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
     at android.app.Activity.startActivityForResult(Activity.java:4231)
     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
     at android.app.Activity.startActivity(Activity.java:4568)
     at android.app.Activity.startActivity(Activity.java:4536)
     at eddgroceryapp.cartcourse2.Activities.MainActivity$1.onClick(MainActivity.java:58)
     at android.view.View.performClick(View.java:5698)
     at android.widget.TextView.performClick(TextView.java:10850)
     at android.view.View$PerformClick.run(View.java:22523)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:158)
     at android.app.ActivityThread.main(ActivityThread.java:7230)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-01 13:51:31.951 1345-1345/eddgroceryapp.cartcourse2 I/Process: Sending signal. PID: 1345 SIG: 9

Upvotes: 0

Views: 89

Answers (2)

Sagar V.S.
Sagar V.S.

Reputation: 246

It seems you forget to declare your activity in Android Manifest file. So, all activities from your project must be declare in manifest file.

Add below code into your manifest file :

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

You can also set other properties like your activity orientation from here, like this :

<activity
        android:name=".BeginShoppingScreen"
        android:screenOrientation="portrait">
</activity>

Upvotes: 0

Firoz Memon
Firoz Memon

Reputation: 4650

Did you add <activity> tag for BeginShoppingScreen in Manifest.xml like below:

<activity
 android:name="your.package.name.BeginShoppingScreen"
 android:label="@string/app_name"
/>

EDIT:

As per your logs:

android.content.ActivityNotFoundException: Unable to find explicit activity class {eddgroceryapp.cartcourse2/eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen}; have you declared this activity in your AndroidManifest.xml?

Add below code in you Manifest file:

<activity
 android:name="eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen"
 android:label="@string/app_name"
/>

Upvotes: 2

Related Questions