2014-11-07 52 views
1

回答(在底部)我的按钮崩溃我在eclipse Android项目:ActivityNotFoundException

所以我一直在试图让我的按钮,让我去到下一个屏幕。 我在eclipse中编程。 这是一个基本登录按钮,允许我转到登录屏幕。 对于这篇较长的文章,我表示歉意,但我觉得现在提供的所有信息都很好。

我想知道,如果是我的设备,我用它来运行时,我按播放。

在我的logcat底部的错误是:

(前几logcat的线)

11-06 18:08:59.973: PID 324 TID 324 D/AndroidRuntime(324): Shutting down VM 
11-06 18:08:59.973: W/dalvikvm(324): threadid=1: thread exiting with uncaught exception  
(group=0x4001d800) 
11-06 18:08:59.983: E/AndroidRuntime(324): FATAL EXCEPTION: main 
11-06 18:08:59.983: E/AndroidRuntime(324): android.content.ActivityNotFoundException: Unable to 
find explicit activity class {com.example.coffeeshop/com.example.coffeeshop.Login}; have you 
declared this activity in your AndroidManifest.xml? 

这些是前几行,因为它转到错误。

所以我RES /布局/ activity_main代码: (请注意,“创建帐户”第二个按钮不会是我试图让登录去登录屏幕的问题。)

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".Main" 
    android:background="#15BCA8" 
    android:id="@+id/activity_main"> 
<Button 
    android:id="@+id/createbtn" 
    android:layout_width="2200dp" 
    android:layout_height="70dp" 
    android:padding="10dp" 
    android:textSize="25sp" 
    android:textStyle="bold|normal" 
    android:text="@string/create_account" 
    android:textColor="#000000" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="44dp" 
    android:background="@drawable/roundedbutton" /> 
<Button 
    android:id="@+id/loginbtn" 
    android:layout_width="2200dp" 
    android:layout_height="80dp" 
    android:layout_above="@+id/createbtn" 
    android:layout_alignStart="@+id/createbtn" 
    android:layout_alignLeft="@+id/createbtn" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="29dp" 
    android:padding="10dp" 
    android:text="@string/log_in" 
    android:textColor="#000000" 
    android:textSize="30sp" 
    android:textStyle="bold|normal" 
    android:typeface="sans" 
    android:background="@drawable/roundedbutton" 
    /> 
</RelativeLayout> 

并且这个布局假设会导致login.xml。此外,圆形按钮将在最后发布,但我不认为这是一个问题。 login.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:background="#15BCA8" 
android:id="@+id/loginlayout"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textSize="60sp" 
    android:text="Login" 
    android:id="@+id/textView" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="61dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Username" 
    android:id="@+id/button" 
    android:textColor="#000000" 
    android:background="#15BCA8" 
    android:layout_marginTop="65dp" 
    android:layout_toStartOf="@+id/textView" 
    android:layout_below="@+id/textView" 
    android:layout_toLeftOf="@+id/textView" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Password" 
    android:id="@+id/button2" 
    android:textColor="#000000" 
    android:background="#15BCA8" 
    android:layout_marginTop="58dp" 
    android:layout_below="@+id/editText" 
    android:layout_alignLeft="@+id/button" 
    android:layout_alignStart="@+id/button" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:ems="10" 
    android:id="@+id/editText" 
    android:layout_below="@+id/button" 
    android:layout_alignLeft="@+id/button2" 
    android:layout_alignStart="@+id/button2" 
    android:background="#FFFFFF" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:ems="10" 
    android:id="@+id/editText2" 
    android:layout_below="@+id/button2" 
    android:layout_alignLeft="@+id/button2" 
    android:layout_alignStart="@+id/button2" 
    android:background="#FFFFFF" /> 

<Button 
    android:id="@+id/sbmtbtn" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_alignEnd="@+id/editText2" 
    android:layout_alignRight="@+id/editText2" 
    android:layout_below="@+id/editText2" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/roundedbutton" 
    android:text="Submit" 
    android:textSize="24sp" /> 

</RelativeLayout> 

所以这似乎相对好,是吗?

这里是我MainActivity.java类:

package com.example.coffeeshop; 

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


    public class MainActivity extends Activity { 

    Button button; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     addListenerOnButton(); 
    } 

    private void addListenerOnButton() { 

     final Context context = this; 

     button = (Button) findViewById(R.id.loginbtn); 
     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent intent = new Intent(context, Login.class); 
       startActivity(intent); 

      } 

     }); 
    } 
} 

而且应该链接我对我的登录界面,在这里编码:

package com.example.coffeeshop; 

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

public class Login extends Activity { 

    Button button; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 
     addListenerOnButton(); 
    } 

    private void addListenerOnButton() { 

     final Context context = this; 

     button = (Button) findViewById(R.id.sbmtbtn); 
     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent(context, MainMenu.class); 
       startActivity(intent); 
      } 
     }); 
    } 
} 

为了好玩,这里是我的 “roundedbutton.xml”文件被我的xml文件调用。 这里应该没有问题。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" android:padding="10dp"> 
<!-- solid android:color="#834672"--> 
<gradient android:startColor="#834672" 
      android:endColor="#718C93" 
      android:angle="270" /> 
<stroke android:width="5px" android:color="#06302F" /> 
<corners android:radius="10dp"/> 
</shape> 

现在我不明白为什么我的程序崩溃了。

好的。我现在已经为我解决了一个问题。我没有意识到这个清单,这个清单立即用他的答案中“快速蜗牛”留下的代码解决。

+0

显示error.did您将活动添加到清单? – 2014-11-07 00:13:02

+1

当报告Android崩溃时,最重要的事情是logcat的输出(参见下面的第三个选项卡或eclipse IDE中的一个) – German 2014-11-07 00:17:34

+0

@German 谢谢,我更新了这个错误,抱歉,仍然学习适当的计算器问题礼仪。 – 2014-11-07 00:28:33

回答

1

以及

ActivityNotFoundException 

意味着你还没有宣布在AndroidManifest文件这一活动。需要声明的所有活动清单文件

错误正是你说的问题

find explicit activity class {com.example.coffeeshop/com.example.coffeeshop.Login}; have you 
declared this activity in your AndroidManifest.xml? 

加入这一行清单文件

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

finaly清单应该看起来像//例如

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.coffeeshop"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".MainActivity" android:label="@string/app_name"></activity> 

     <activity android:name=".Login"></activity> <!--add this line --> 

    </application> 
</manifest> 
+1

嘿人。谢谢!我明白你现在说的话。我是Android开发新手,所以我感谢您的耐心。我现在在工作,所以我会在我回家时更新它,并尽快报告。再次感谢。 – 2014-11-07 04:58:23

+1

我得到它的工作,我也将它添加到其他活动。这一切都有效。不胜感激。我打算保留这篇文章,稍后再更新,然后再处理它。 – 2014-11-07 21:42:16

2

根据你的logcat,问题是你还没有定义你的活动在清单文件中。

清单文件向Android系统提供有关您的应用程序的重要信息,该系统必须具有该信息才能运行该应用程序的任何代码。在应用程序运行之前,系统必须知道应用程序中存在哪些活动,并通过清单文件知道。如果您错过了清单文件中的任何活动,那么系统会假定该活动不存在。现在,当应用程序运行时,它指向一些系统无法识别的活动。因此,它崩溃。你可以阅读更多关于Android清单文件在这里http://developer.android.com/guide/topics/manifest/manifest-intro.html

我不能upvote其他我会与@fast蜗牛的答案,我认为这将解决您的问题。

+0

我同意,并感谢您的信息。我仍然需要更新它。但这应该做到这一点。 – 2014-11-07 04:59:48