我正在寫一個非常簡單的轉換器應用程序,它在我的手機上運行良好,但之後當我斷開連接並重新連接到筆記本電腦時,它停止工作,顯示上述消息。不幸的是,<App name>已停止工作。 Android?Eclipse應用程序
下面的代碼:
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新手,這是我的第一個應用程序,所以可能是我做的一個非常愚蠢的錯誤,但請解釋錯誤是什麼以及如何解決它?
在此先感謝!
發佈您的XML佈局。 – Raghunandan
'android.widget.TextView不能轉換爲android.widget.EditText' - 很明顯。你已經在XML中聲明瞭一些TextView,並試圖將它轉換爲代碼中的EditText。 – Simon
如何修改該錯誤? –