每当我开始新的活动时,我的应用都会崩溃

时间:2016-11-01 17:45:15

标签: android android-activity

我已经开始学习如何使用Google的教程为Android编程,但我遇到了一个很大的问题。

我有一个按钮,点击它时应该将用户切换到另一个屏幕(活动),但每当我运行应用程序时,它都会崩溃。

这是在单击Button时应该启动下一个Activity的代码:

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

        }

    });
}

onCreate()方法中调用此方法,以便侦听器处于活动状态。

这是主要活动的XML:

<?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>

这是另一个Activity的XML:

<?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>

崩溃日志:

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

2 个答案:

答案 0 :(得分:2)

您是否在<activity>中为BeginShoppingScreen添加了Manifest.xml代码,如下所示:

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

编辑:

根据您的日志:

  

android.content.ActivityNotFoundException:无法找到显式   活动课   {eddgroceryapp.cartcourse2 / eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen};   你有没有在AndroidManifest.xml中声明这个活动?

在Manifest文件中添加以下代码:

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

答案 1 :(得分:0)

您似乎忘了在Android Manifest文件中声明您的活动。 因此,项目中的所有活动都必须在清单文件中声明。

将以下代码添加到您的清单文件中:

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

您还可以从此处设置其他属性,例如您的活动方向,如下所示:

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