show view>devices)。 关闭 E-6ren">
gpt4 book ai didi

java - 错误: "The application has stopped unexpectedly. Please try again.'

转载 作者:行者123 更新时间:2023-12-02 07:28:19 25 4
gpt4 key购买 nike

我的应用程序前几次运行良好,现在决定“意外”停止。我尝试了以下方法看能否解决,但没有效果:

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

我注意到,有一半的时间,Eclipse 似乎总是因为一些断开连接问题而卡在尝试启动 HOME 上(为什么它一直这样做变得越来越严重),所以我总是最终再次点击“运行”并开始另一个模拟器(请参阅下面的控制台对话框)。

 [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;
}
};
};
}

这是主要 Activity 的 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>

感谢您的浏览。

最佳答案

您可以通过查看紧跟在以 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);

类转换异常?您标识的 View 不是复选框。

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

将其与其他导入一起放在代码顶部

import com.example.costaload.R;

另一件事:我不确定这是否是您的 Intent ,而是 switch 语句中第一个 casebreak; 命令位于 if block 内。如果 ophours 小于 11,您的 switch 语句将进入下一个情况。

关于java - 错误: "The application has stopped unexpectedly. Please try again.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com