Java 错误:";应用程序意外停止。请再试一次;
我的应用程序最初几次运行良好,现在决定“意外”停止。我尝试了以下方法,看看是否可以解决它,但没有结果:Java 错误:";应用程序意外停止。请再试一次;,java,android,Java,Android,我的应用程序最初几次运行良好,现在决定“意外”停止。我尝试了以下方法,看看是否可以解决它,但没有结果: 从Eclipse中重新启动adb(使用窗口>显示视图>设备) 关闭Eclipse,手动终止adb,然后重新启动它 删除已安装的应用程序,然后重新安装 我注意到,有一半时间,Eclipse似乎总是因为一些断开连接的问题而在尝试启动HOME时挂断(为什么它一直这样做变得越来越严重),所以我总是再次点击Run并启动另一个模拟器(请参阅下面的控制台对话框) 所以,你们可以看到我点击RUN,然后5556
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
当我看LOGCAT时,我承认我并不擅长尝试去弄清楚,因为上面有一些东西我无法识别,因为我是一个初学者(例如:“受精卵”?,活套,等等)。换句话说,我无法理解代码最初几次是如何运行良好的,现在正在抛出下面日志中显示的内容。我唯一能识别的是android按钮小部件,这是一种强制转换例外。奇怪的是,它只是一个按钮侦听器和case结构,用于侦听按下哪个按钮。除此之外,我不知道,行号是我甚至不知道的文件
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
尽管我试图解释这些错误,但我最好的猜测是它与button小部件有关,而其余的看起来是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>
谢谢你看 您可以通过查看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);
类强制转换异常?您标识的视图不是复选框
您的MainActivity似乎没有导入您正在使用的R类。我不确定您使用的是哪个版本的Eclipse,或者它是否以某种奇怪的方式处理了这个问题,但它可能会默认为您不想要的值
将它与其他导入放在代码的顶部
import com.example.costaload.R;
另一件事:我不确定这是否是你的意图,但
中断switch
语句中第一个案例的code>命令位于if块内。如果opours
小于11,您的switch语句将进入下一种情况。本质上,(可能)您拥有的一个“按钮”实际上不是按钮,请确保button1和button2实际上是layout.xml文件中的按钮。您不能(例如)将文本视图强制转换为按钮。您是否检查过活动主布局中是否有按钮类型的button1和button2元素?如果是这样,请尝试清理项目以使ADT重写R文件
当您开始学习Android编程时,这种问题经常发生(例如忘记更改清单等)。确保button1和button2实际上是布局xml文件中的按钮。您的异常意味着您正试图从一个类中强制转换。。说一个按钮。。对于另一个不兼容类型的类(如EditText),您的一个按钮不是真正的按钮,请参阅MainActivity.java第28行,了解第28行中您引用的不是真正按钮的id的罪犯。根据提供的信息,我同意。OP的XML可能是
而不是
。虽然复选框总是一个按钮,但按钮并不总是一个复选框……下面是主XML中的checkBox1的样子:在您的问题中发布XML文件。这很有帮助!然而,现在模拟器中有一个弹出窗口“使用:应用程序信息等完成操作”。当我单击按钮1(应该是计算)时,有人问我想如何完成该操作。我以前从未见过这样的情况发生在按钮进行计算时。我知道按钮2必须用浏览器完成。程序是否有可能读取按钮2的意图行?是的,主布局中确实有所有按钮和复选框ID,并且类型正确。按钮1和按钮2都是布局文件中的按钮。