0

我是一个 android 初学者(我的第一篇文章)并且在运行这个 bmi 计算器教程时崩溃了:http ://android-meda.blogspot.co.uk/2012/01/writing-your-first-android-app- body.html?m=1

在我的手机上启动时,屏幕变黑并显示“BMI 已不幸停止”的消息。我不确定在哪里诊断问题,因此非常感谢任何帮助。谢谢

04-04 14:14:44.750: E/SensorManager(24999): thread start
04-04 14:14:44.755: D/SensorManager(24999): registerListener :: handle = 0  name=  LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42b75490
04-04 14:14:44.760: D/SensorManager(24999): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42b75490
04-04 14:14:44.760: D/Sensors(24999): Remain listener = Sending .. normal delay 200ms
04-04 14:14:44.760: I/Sensors(24999): sendDelay --- 200000000
04-04 14:14:44.765: D/SensorManager(24999): JNI - sendDelay
04-04 14:14:44.765: I/SensorManager(24999): Set normal delay = true
04-04 14:14:44.815: D/SensorManager(24999): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42b75490
04-04 14:14:44.870: D/libEGL(24999): loaded /system/lib/egl/libEGL_mali.so
04-04 14:14:44.875: D/libEGL(24999): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-04 14:14:44.880: D/libEGL(24999): loaded /system/lib/egl/libGLESv2_mali.so
04-04 14:14:44.885: D/(24999): Device driver API match
04-04 14:14:44.885: D/(24999): Device driver API version: 10
04-04 14:14:44.885: D/(24999): User space API version: 10 
04-04 14:14:44.885: D/(24999): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
04-04 14:14:44.910: D/OpenGLRenderer(24999): Enabling debug mode 0
04-04 14:14:44.985: W/IInputConnectionWrapper(24999): showStatusIcon on inactive InputConnection
04-04 14:14:52.235: D/SensorManager(24999): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42b75490
04-04 14:14:52.235: D/Sensors(24999): Remain listener = Sending .. normal delay 200ms
04-04 14:14:52.235: I/Sensors(24999): sendDelay --- 200000000
04-04 14:14:52.235: D/SensorManager(24999): JNI - sendDelay
04-04 14:14:52.235: I/SensorManager(24999): Set normal delay = true
04-04 14:14:52.240: D/SensorManager(24999): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42b78da8
04-04 14:14:52.260: W/IInputConnectionWrapper(24999): getSelectedText on inactive InputConnection
04-04 14:14:52.265: W/IInputConnectionWrapper(24999): setComposingText on inactive InputConnection
04-04 14:14:52.265: W/IInputConnectionWrapper(24999): getExtractedText on inactive InputConnection

04-04 14:14:53.420: D/AndroidRuntime(24999): Shutting down VM
04-04 14:14:53.420: W/dalvikvm(24999): threadid=1: thread exiting with uncaught exception (group=0x41f052a0)
04-04 14:14:53.430: E/AndroidRuntime(24999): FATAL EXCEPTION: main
04-04 14:14:53.430: E/AndroidRuntime(24999): java.lang.IllegalStateException: Could not execute method of the activity
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.view.View$1.onClick(View.java:3691)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.view.View.performClick(View.java:4211)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.view.View$PerformClick.run(View.java:17267)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.os.Handler.handleCallback(Handler.java:615)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.os.Looper.loop(Looper.java:137)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at java.lang.reflect.Method.invoke(Method.java:511)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at dalvik.system.NativeStart.main(Native Method)
04-04 14:14:53.430: E/AndroidRuntime(24999): Caused by: java.lang.reflect.InvocationTargetException
04-04 14:14:53.430: E/AndroidRuntime(24999):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at java.lang.reflect.Method.invoke(Method.java:511)
04-04 14:14:53.430: E/AndroidRuntime(24999):    at android.view.View$1.onClick(View.java:3686)
04-04 14:14:53.430: E/AndroidRuntime(24999):    ... 11 more
04-04 14:14:53.430: E/AndroidRuntime(24999): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
04-04 14:14:53.430: E/AndroidRuntime(24999):    at com.example.bmi.MainActivity.calculateClickHandler(MainActivity.java:22)
04-04 14:14:53.430: E/AndroidRuntime(24999):    ... 14 more
4

1 回答 1

0

您正试图将 TextView 转换为 EditText:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

检查您的代码中是否有 (TextView) variable = (EditText) findViewById(..)...

问题出在 MainActivity.java 文件的第 22 行。

如果您分析粘贴的堆栈跟踪:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
at com.example.bmi.MainActivity.calculateClickHandler(MainActivity.java:22)

它指出了问题所在(MainActivity.java:22)。这意味着 MainActivity.java 文件中的第 22 行。

更好的是,如果您找不到问题,请在此处粘贴代码片段,以便我可以更具体地指出问题。您应该从 onClick 侦听器中发布代码

EditText 用于编辑文本,TextView 是用于显示文本的组件。因此它们不兼容。第一个是可编辑的表单元素,而后者仅用于显示目的。你不能把一个扔进另一个。

于 2013-04-04T14:41:27.320 回答