1

我正在编写一个非常简单的转换器应用程序,它在我的手机上运行了一段时间,但是当我断开它并重新连接到笔记本电脑时,它停止工作并显示上述消息。

这是代码:

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));



            }
        });
    }



}

这是日志猫

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 新手,这是我的第一个应用程序,所以可能是我正在做的一个非常愚蠢的错误,但请解释错误是什么以及如何修复它?

提前致谢!

4

1 回答 1

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

第 18 行(计数...)这是您的 xml 中的 TextView。将其更改为 EditText

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

于 2013-07-23T18:35:37.120 回答