android - 不幸的是 <App name> 已经停止工作AndroidEclipse应用程序?

  显示原文与译文双语对照的内容
0 0

我写的是一个非常简单的转换器应用程序,它在我的手机上工作,但是断开了。

下面是代码:

package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
 @Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 final EditText editCentimeters = (EditText) findViewById(R.id.editCent);
 final EditText editInches = (EditText) findViewById(R.id.editInch);
 Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
 buttonConvert.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
 double Centi = Double.valueOf(editCentimeters.getText().toString());
 double Inches= Centi * 0.3937007 ;
 editInches.setText(String.valueOf(Inches));
 }
 });
 }
}

这是 logcat

07-23 23:54:42.624: D/dalvikvm(12601): Late-enabling CheckJNI
07-23 23:54:43.745: D/AndroidRuntime(12601): Shutting down VM
07-23 23:54:43.745: W/dalvikvm(12601): threadid=1: thread exiting with uncaught exception (group=0x40a72228)
07-23 23:54:43.765: E/AndroidRuntime(12601): FATAL EXCEPTION: main
07-23 23:54:43.765: E/AndroidRuntime(12601): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.os.Looper.loop(Looper.java:156)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.ActivityThread.main(ActivityThread.java:4987)
07-23 23:54:43.765: E/AndroidRuntime(12601): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 23:54:43.765: E/AndroidRuntime(12601): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 23:54:43.765: E/AndroidRuntime(12601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 23:54:43.765: E/AndroidRuntime(12601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 23:54:43.765: E/AndroidRuntime(12601): at dalvik.system.NativeStart.main(Native Method)
07-23 23:54:43.765: E/AndroidRuntime(12601): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-23 23:54:43.765: E/AndroidRuntime(12601): at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:18)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.Activity.performCreate(Activity.java:4538)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-23 23:54:43.765: E/AndroidRuntime(12601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-23 23:54:43.765: E/AndroidRuntime(12601):. . . 11 more

谁能告诉我这个问题是什么? 我是Android的新手,这是我的第一个应用,可以能是我的错误,但是请解释一下错误是什么。

提前致谢!

时间:原作者:6个回答

0 0
 final EditText editCentimeters = (EditText) findViewById(R.id.editCent);

第 18行( 已经计数。) 这是xml中的一个。 将它更改为 EditText

如果你在xml中确定它是 EditText 。 Eclipse -> 项目-> 清理。

原作者:
...