2012-11-06 86 views
0

我的应用程序细跑第几次了,现在已决定停止我曾尝试以下方法来看看我是否能解决这个问题,但无济于事“意外”:错误:“应用程序意外停止,请重试。”

  1. 在Eclipse重新启动ADB (使用窗口>显示视图>设备)。
  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> 

谢谢你看看。

+1

确保button1和button2实际上是布局xml文件中的按钮。你的异常意味着你正试图从一个类转换..说一个按钮..到另一个类(如EditText)不是一个兼容的类型 – dymmeh

+0

你的一个按钮不是一个真正的按钮,请参阅MainActivity.java行28为罪魁祸首 – petey

+0

在第28行中,您引用的是一个不是真正的Button的ID。 – sabadow

回答

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; 

另一件事:我不知道,如果这是你的意图,但在你的switch语句中的第casebreak;命令里面一个if块。如果ophours小于11,则您的switch语句将转入下一个案例。

+1

提供的信息,我同意。 OP的XML可能是'

+0

下面是checkBox1在主XML中的样子: Shawner

+0

在您的问题中发布XML文件。 – GraphicsMuncher

0

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

+0

button1和button2都是布局文件中的按钮。 – Shawner

0

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

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

+0

是的,主布局确实具有所有按钮和复选框ID以及它们的正确类型。 – Shawner

+0

清理了该项目并仍然出现错误。 :/ – Shawner

相关问题