2012-11-06 84 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

相關問題