简体   繁体   English

不幸的是app已经停止工作

[英]Unfortunately app has stopped working

I am new to android application development. 我是Android应用程序开发的新手。 I was doing this tutorial app.It's a very simple one. 我正在做这个教程app.It非常简单。 It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." 它添加一个并从计数器中减去一个。当我在模拟器中运行它时,它说“不幸的是教程已停止工作”。 There are no errors in the code. 代码中没有错误。 The API level is 17. Please help me out. API级别是17.请帮帮我。

Code for java: 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;
  }
}

Layout code in xml : 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>

Here is the logcat : 这是logcat:

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
display= (Button) findViewById(R.id.text);

should be 应该

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

Since display is supposed to reference a TextView instance, but you're explictly casting into a Button , and these are not compatible types. 因为display应该引用一个TextView实例,但你明显地转换为一个Button ,而这些不是兼容的类型。

As you got the answer from AC this time, but for next time in android application development a important suggestion: 正如你这次从AC得到了答案,但是下次在android应用程序开发中提出了一个重要的建议:

Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error. 总是看到logcat是否有错误,并看到“由...引起的”标记,它指出问题的原因是什么,有足够的细节,另请参阅导致该错误的行号。 And try to find what can be wrong with that line of code. 并尝试找出该行代码可能出错的地方。

For example: in your logcat it is showing- 例如:在你的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)

So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button . 所以你可以尝试阅读日志,你会明白你的文件Startingpoint.java位于line 22 ,它位于onCreate方法,错误是android.widget.TextView cannot be cast to android.widget.Button So you can easily remove your errors without any help. 因此,您无需任何帮助即可轻松删除错误。 Hope that helps not only you current problem but prevented your future time and efforts. 希望这不仅可以帮助您解决当前的问题,还可以防止您未来的时间和精力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM