错误:“应用程序意外停止。请再试一次。”

时间:2012-11-06 18:05:20

标签: java android

我的应用程序在前几次运行正常,现在已经决定“意外停止”。我尝试了以下方法来查看是否可以解决它,但无济于事:

  1. 从Eclipse中重新启动adb(使用Window> show view> devices)。
  2. 关闭Eclipse,手动终止adb然后重新启动它。
  3. 删除已安装的应用,然后重新安装。
  4. 我注意到有一半的时间,Eclipse因为一些断线问题(为什么它一直在做这种情况变得更加严重)似乎总是挂起来试图启动HOME,所以我总是最终再次点击Run并启动另一个模拟器(请参阅下面的控制台对话框)。

     [2012-11-06 11:58:03 - CostALoad] ------------------------------
     [2012-11-06 11:58:03 - CostALoad] Android Launch!
     [2012-11-06 11:58:03 - CostALoad] adb is running normally.
     [2012-11-06 11:58:03 - CostALoad] Performing com.example.costaload.MainActivity activity launch
     [2012-11-06 11:58:04 - CostALoad] Automatic Target Mode: launching new emulator with compatible AVD 'myavd'
     [2012-11-06 11:58:04 - CostALoad] Launching a new emulator with Virtual Device 'myavd'
     [2012-11-06 11:58:06 - CostALoad] New emulator found: emulator-5554
     [2012-11-06 11:58:06 - CostALoad] Waiting for HOME ('android.process.acore') to be launched...
     [2012-11-06 11:58:40 - CostALoad] HOME is up on device 'emulator-5554'
     [2012-11-06 11:58:40 - CostALoad] Uploading CostALoad.apk onto device 'emulator-5554'
     [2012-11-06 11:58:42 - CostALoad] Installing CostALoad.apk...
     [2012-11-06 11:59:01 - CostALoad] Success!
     [2012-11-06 11:59:02 - CostALoad] Starting activity com.example.costaload.MainActivity on device 
       emulator-  5554
     [2012-11-06 11:59:04 - CostALoad] ActivityManager: Starting: Intent { act=android.intent.action.MAIN
      cat=[android.intent.category.LAUNCHER] cmp=com.example.costaload/.MainActivity }
     [2012-11-06 12:17:00 - CostALoad] ------------------------------
     [2012-11-06 12:17:00 - CostALoad] Android Launch!
     [2012-11-06 12:17:00 - CostALoad] adb is running normally.
     [2012-11-06 12:17:00 - CostALoad] Performing com.example.costaload.MainActivity activity launch
     [2012-11-06 12:17:01 - CostALoad] Automatic Target Mode: launching new emulator with compatible AVD 'myavd'
     [2012-11-06 12:17:01 - CostALoad] Launching a new emulator with Virtual Device 'myavd'
     [2012-11-06 12:17:25 - CostALoad] New emulator found: emulator-5554
     [2012-11-06 12:17:25 - CostALoad] Waiting for HOME ('android.process.acore') to be launched...
     [2012-11-06 12:18:43 - CostALoad] emulator-5554 disconnected! Cancelling 'com.example.costaload.MainActivity 
        activity launch'!
     [2012-11-06 12:20:07 - CostALoad] ------------------------------
     [2012-11-06 12:20:07 - CostALoad] Android Launch!
     [2012-11-06 12:20:07 - CostALoad] adb is running normally.
     [2012-11-06 12:20:07 - CostALoad] Performing com.example.costaload.MainActivity activity launch
     [2012-11-06 12:20:07 - CostALoad] Automatic Target Mode: launching new emulator with compatible AVD 'myavd'
     [2012-11-06 12:20:07 - CostALoad] Launching a new emulator with Virtual Device 'myavd'
     [2012-11-06 12:20:22 - Emulator] WARNING: Data partition already in use. Changes will not persist!
     [2012-11-06 12:20:22 - Emulator] WARNING: Cache partition already in use. Changes will not persist!
     [2012-11-06 12:20:23 - CostALoad] New emulator found: emulator-5556
     [2012-11-06 12:20:23 - CostALoad] Waiting for HOME ('android.process.acore') to be launched...
     [2012-11-06 12:22:07 - CostALoad] HOME is up on device 'emulator-5556'
     [2012-11-06 12:22:07 - CostALoad] Uploading CostALoad.apk onto device 'emulator-5556'
     [2012-11-06 12:22:08 - CostALoad] Installing CostALoad.apk...
     [2012-11-06 12:22:26 - CostALoad] Success!
     [2012-11-06 12:22:27 - CostALoad] Starting activity com.example.costaload.MainActivity on device 
        emulator-5556
     [2012-11-06 12:22:31 - CostALoad] ActivityManager: Starting: Intent { act=android.intent.action.MAIN 
        cat=[android.intent.category.LAUNCHER] cmp=com.example.costaload/.MainActivity }
    

    所以,你可以看到我在哪里点击RUN然后5556开始,上传然后停止。这一直都在进行,我上面描述的所有步骤似乎都没有解决问题。

    当我看到LOGCAT时,我承认我并不擅长弄清楚,因为那里的东西我无法识别,因为我是初学者(例如:“zygote”??,looper等。 )。换句话说,我无法理解代码如何在前几次工作正常,现在正在抛出下面的日志中显示的内容。我唯一认识到的是android按钮小部件,它是某种类型的强制转换异常。奇怪,因为它只是一个按钮监听器和案例结构来监听按下哪个按钮。除此之外,我不知道,行号是针对我甚至不知道的文件。

     11-06 12:22:33.080: D/ddm-heap(223): Got feature list request
     11-06 12:22:34.350: D/AndroidRuntime(223): Shutting down VM
     11-06 12:22:34.350: W/dalvikvm(223): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
     11-06 12:22:34.359: E/AndroidRuntime(223): Uncaught handler: thread main exiting due to uncaught exception
     11-06 12:22:34.369: E/AndroidRuntime(223): java.lang.RuntimeException: Unable to start activity
       ComponentInfo{com.example.costaload/com.example.costaload.MainActivity}: java.lang.ClassCastException:   
       android.widget.Button
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.performLaunchActivity
       (ActivityThread.java:2496)
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.handleLaunchActivity
       (ActivityThread.java:2512)
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.access$2200
       (ActivityThread.java:119)
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread$H.handleMessage
       (ActivityThread.java:1863)
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.os.Handler.dispatchMessage(Handler.java:99)
     11-06 12:22:34.369: E/AndroidRuntime(223):     at android.os.Looper.loop(Looper.java:123) 
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.main(ActivityThread.java:4363)
     11-06 12:22:34.369: E/AndroidRuntime(223):     at java.lang.reflect.Method.invokeNative(Native Method)
     11-06 12:22:34.369: E/AndroidRuntime(223):     at java.lang.reflect.Method.invoke(Method.java:521)
     11-06 12:22:34.369: E/AndroidRuntime(223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
       (ZygoteInit.java:860)
     11-06 12:22:34.369: E/AndroidRuntime(223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
     11-06 12:22:34.369: E/AndroidRuntime(223): at dalvik.system.NativeStart.main(Native Method)
     11-06 12:22:34.369: E/AndroidRuntime(223): Caused by: java.lang.ClassCastException: android.widget.Button
     11-06 12:22:34.369: E/AndroidRuntime(223): at com.example.costaload.MainActivity.onCreate
       (MainActivity.java:28)
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.Instrumentation.callActivityOnCreate
       (Instrumentation.java:1047)
     11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.performLaunchActivity
       (ActivityThread.java:2459)
     11-06 12:22:34.369: E/AndroidRuntime(223):     ... 11 more
     11-06 12:22:34.419: I/dalvikvm(223): threadid=7: reacting to signal 3
     11-06 12:22:34.419: E/dalvikvm(223): Unable to open stack trace file '/data/anr/traces.txt': 
       Permission denied
     11-06 12:27:34.529: I/Process(223): Sending signal. PID: 223 SIG: 9
    

    尝试我解释错误,我最好的猜测是它与按钮小部件有关,其余的看起来像Java本身由于某些东西而急转直下。 LOGCAT引用的东西我不知道它是什么或从未触及过。我还能做些什么来阻止这一切发生?我已经在下面包含了我的代码,就像我说的那样,它已经运行过,现在我无法看到问题是什么,或者我的项目文件中是否存在导致它的内容:

     package com.example.costaload;
    
     import java.text.DecimalFormat;
     import java.text.NumberFormat;
    
     import android.os.Bundle;
     import android.app.Activity;
     import android.content.Intent;
     import android.widget.CheckBox;
     import android.widget.EditText;
     import android.widget.Button;
     import android.widget.TextView;
     import android.view.View;
    
    
    public class MainActivity extends Activity {
    EditText mile,diesel;
    Button button1, button2;
    TextView tv, tv2, tv3;
    private double x, y, z, costper, gallon, litres, ophours, stopdrive;
    CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
    NumberFormat format  = NumberFormat.getCurrencyInstance();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mile= (EditText) findViewById(R.id.mile);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        checkBox4 = (CheckBox) findViewById(R.id.checkBox4);         
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        tv = (TextView) findViewById(R.id.cost);
        tv2 = (TextView) findViewById(R.id.cpm);
        tv3 = (TextView) findViewById(R.id.gallons);
        diesel= (EditText) findViewById(R.id.diesel);
             }
    
     private OnClickListener onClickListener = new OnClickListener() {
       @Override
       public void onClick(final View v) {
           switch(v.getId()){
             case R.id.button1:
                x=Double.parseDouble(mile.getText().toString());
                y=Double.parseDouble(diesel.getText().toString());
                if (checkBox2.isChecked()) {
                     x=x*2;
                 }
                if (checkBox1.isChecked()) {
                    x=x*0.62137;
                }
                ophours = 0;
                ophours = (x/55)+2;
                if (ophours>=11){
                    stopdrive = (ophours/10)-1;
                    if (stopdrive>1){
                        ophours = ophours + (stopdrive*10);
                }
                gallon = x/5.5;
                if (checkBox4.isChecked()) {
                    gallon = gallon + (ophours*1.1);
                }
                if (checkBox3.isChecked()) {
                    litres = gallon*3.785;
                    tv3.setText(new DecimalFormat("####.##").format(litres)+"L");
                }
                z=(gallon*y)+(x*0.655);
                costper=z/x;
                tv.setText(format.format(z));
                tv2.setText(format.format(costper)+"/mile"); 
                tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
             break;}
             case R.id.button2:
                Intent browserIntent = 
                new Intent(Intent.ACTION_VIEW, Url.parse("http://www.ratenroll.com"));
                                     startActivity(browserIntent);   
             break;
       }
       };
       };
     }                      
    

    这是主要活动的XML,我很难过,因为所有按钮都是按钮等等:

     <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" >
    
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="# of miles"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    
     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="40dp"
        android:text="Price of diesel"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    
     <EditText
        android:id="@+id/diesel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/mile"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="numberDecimal" />
    
     <EditText
        android:id="@+id/mile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/checkBox3"
        android:ems="10"
        android:inputType="numberDecimal" >
    
        <requestFocus />
      </EditText>
    
     <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Visit www.ratenroll.com" />
    
     <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/diesel"
        android:layout_marginTop="14dp"
        android:text="Kilometres (not miles)" />
    
     <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox1"
        android:layout_alignBottom="@+id/checkBox1"
        android:layout_alignParentRight="true"
        android:text="Round trip" />
    
     <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox1"
        android:text="Litres (not gallons)" />
    
     <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox3"
        android:layout_alignBottom="@+id/checkBox3"
        android:layout_alignParentRight="true"
        android:text="Reefer load" />
    
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox3"
        android:layout_marginTop="50dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="Compute" />
    
     <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="26dp"
        android:text="Total Cost"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:text="Cost per mile"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <TextView
        android:id="@+id/cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView4"
        android:layout_alignParentRight="true"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <TextView
        android:id="@+id/cpm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignParentRight="true"
        android:text="   "
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="Fuel required"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
      <TextView
        android:id="@+id/gallons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignParentRight="true"
        android:text="   "
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     </RelativeLayout>
    

    感谢您一看。

3 个答案:

答案 0 :(得分:2)

您可以通过查看紧跟在Caused by:

开头的行之后的LogCat行找到导致错误的行

基本上格式是这样的:

Caused by: (error type)
at:problematic method:line number

所以对你来说答案就在这里:

11-06 12:22:34.369: E/AndroidRuntime(223): Caused by: java.lang.ClassCastException: android.widget.Button
11-06 12:22:34.369: E/AndroidRuntime(223): at com.example.costaload.MainActivity.onCreate (MainActivity.java:28)

MainActivity类中onCreate方法的第28行是问题所在。

checkBox1 = (CheckBox) findViewById(R.id.checkBox1);

类强制转换异常?您识别的视图不是CheckBox。

您的MainActivity似乎没有导入R类(您正在使用它)。我不确定您拥有哪个版本的Eclipse,或者它是否以某种奇怪的方式处理它,但它可能默认为您不想要的某些值。

使用其他导入

将其放在代码的顶部
import com.example.costaload.R;

另一件事:我不确定这是否是您的意图,但break;语句中第一个case的{​​{1}}命令位于if块内。如果switch小于11,则您的switch语句将进入下一个案例。

答案 1 :(得分:0)

基本上,(可能)你拥有的一个“按钮”实际上并不是一个按钮,请确保button1和button2实际上是layout.xml文件中的按钮。您不能将(例如)TextView转换为Button。

答案 2 :(得分:0)

您是否在activity_main布局中检查过您是否具有Button类型的button1和button2元素?如果是这样,请尝试清理项目以使ADT重写R文件。

当您开始学习Android编程时(例如忘记更改清单等),这种问题经常发生。