【问题标题】:Unfortunately app has stopped working不幸的是应用程序已停止工作
【发布时间】:2013-02-16 16:31:35
【问题描述】:

我是 android 应用程序开发的新手。我正在做这个教程应用程序。这是一个非常简单的应用程序。它从计数器中加一并减一。当我在模拟器中运行它时,它说“不幸的是教程已停止工作。”代码中没有错误。 API级别是17。请帮帮我。

java代码:

public class Startingpoint extends Activity {
  int counter=0;   
  Button add,subtract; 
  TextView display; 

  @Override    
  protected void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.startingpoint);    
    add = (Button) findViewById(R.id.bAdd);    
    subtract= (Button) findViewById(R.id.bSubtract);   
    display= (Button) findViewById(R.id.text);    
    add.setOnClickListener(new View.OnClickListener() {

      @Override   
      public void onClick(View v) {   
        counter++;  
        display.setText("The total is " + counter);   
      }   
    });   

    subtract.setOnClickListener(new View.OnClickListener() {
      @Override 
      public void onClick(View v) {
        counter--;
        display.setText("The total is " + counter);
      }

    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.startingpoint, menu);
    return true;
  }
}

xml中的布局代码:

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

    <TextView android:id="@+id/text"    
              android:layout_width="wrap_content"   
              android:layout_height="wrap_content"
              android:text="Your total is 0"
              android:textSize="35dp"
              android:layout_gravity="center"
              android:gravity="center"/>

    <Button   android:layout_width="250dp"
              android:layout_height="wrap_content"
              android:text="Add One" 
              android:layout_gravity="center" 
              android:textSize="25dp"
              android:id="@+id/bAdd"/>

     <Button  android:layout_width="250dp"
              android:layout_height="wrap_content"
              android:text="Subtract One" 
              android:layout_gravity="center" 
              android:textSize="25dp"
              android:id="@+id/bSubtract"/>
</LinearLayout>

这里是日志:

03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780):  at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780):  at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780):  at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780):  at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780):  ... 11 more

【问题讨论】:

  • 下一次,只需在此处发布您的代码,并使用正确的格式。双倍行距代码(至少对我而言)可读性较差。
  • “代码中没有错误。” - 我认为你的意思是没有编译错误。显然,至少有一个错误。

标签: java android android-layout android-emulator classcastexception


【解决方案1】:
display= (Button) findViewById(R.id.text);

应该是

display= (TextView) findViewById(R.id.text);

由于 display 应该引用 TextView 实例,但您明确地转换为 Button,这些是兼容的类型。

【讨论】:

  • 谢谢,成功了!!另外,在创建新的 Android 虚拟设备时要选择哪个设备?这对我来说似乎很混乱。
  • @user2125722 很高兴听到它成功了!由于您是新手,而且您的问题实际上已经解决,请务必阅读how does accepting an answer work?,以便您了解接受的工作原理。至于您的后续行动 - 这取决于您要使用哪些 API 功能。如果您使用新的 ADT 插件,我会说选择 Nexus 7 或 Galaxy nexus 配置文件。我个人使用 2.2 和 4.1.x 进行测试。如需更多信息,请浏览this
【解决方案2】:

这次你从 A-C 那里得到了答案,但是对于下一次在 android 应用程序开发中的一个重要建议:

始终查看 logcat 是否有错误,并查看“Caused by:”标签,它以足够详细的信息指定问题的原因,还可以查看导致该错误的行号。 并尝试找出那行代码有什么问题。

例如:在您的 logcat 中显示-

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button 
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)

所以你可以尝试阅读日志,你会明白在你的文件Startingpoint.javaline 22 位于onCreate 方法中,错误是android.widget.TextView cannot be cast to android.widget.Button。因此,您无需任何帮助即可轻松删除错误。 希望这不仅可以帮助您解决当前的问题,还可以防止您未来的时间和精力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多