0

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

  1. 从 Eclipse 中重新启动 adb(使用 Window>show view>devices)。
  2. 关闭 Eclipse,手动杀死 adb 然后重新启动它。
  3. 删除已安装的应用程序,然后重新安装。

我注意到一半的时间,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>

谢谢你看。

4

3 回答 3

2

您可以通过查看直接跟随以开头的行的 LogCat 行来找到导致错误的行Caused by:

基本上格式是这样的:

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;第一个命令位于 if 块内。如果小于 11,您的 switch 语句将进入下一个案例。caseswitchophours

于 2012-11-06T18:13:46.163 回答
0

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

于 2012-11-06T18:16:53.247 回答
0

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

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

于 2012-11-06T18:22:28.437 回答