这是我第一次阅读有关如何使用以下 URL 在 Android 上创建应用程序的教程:MyFirstApp
当我运行它时,我通过 LogCat 在堆栈跟踪中得到了一个 ClassCastException,如下所示:
03-02 13:18:49.365: E/AndroidRuntime(1996): FATAL EXCEPTION: main
03-02 13:18:49.365: E/AndroidRuntime(1996): java.lang.IllegalStateException: Could not execute method of the activity
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.view.View$1.onClick(View.java:3044)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.view.View.performClick(View.java:3511)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.view.View$PerformClick.run(View.java:14105)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.os.Handler.handleCallback(Handler.java:605)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.os.Looper.loop(Looper.java:137)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-02 13:18:49.365: E/AndroidRuntime(1996): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 13:18:49.365: E/AndroidRuntime(1996): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 13:18:49.365: E/AndroidRuntime(1996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-02 13:18:49.365: E/AndroidRuntime(1996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-02 13:18:49.365: E/AndroidRuntime(1996): at dalvik.system.NativeStart.main(Native Method)
03-02 13:18:49.365: E/AndroidRuntime(1996): Caused by: java.lang.reflect.InvocationTargetException
03-02 13:18:49.365: E/AndroidRuntime(1996): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 13:18:49.365: E/AndroidRuntime(1996): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 13:18:49.365: E/AndroidRuntime(1996): at android.view.View$1.onClick(View.java:3039)
03-02 13:18:49.365: E/AndroidRuntime(1996): ... 11 more
03-02 13:18:49.365: E/AndroidRuntime(1996): Caused by: java.lang.ClassCastException: com.android.internal.view.menu.ActionMenuItemView cannot be cast to android.widget.TextView
03-02 13:18:49.365: E/AndroidRuntime(1996): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:34)
03-02 13:18:49.365: E/AndroidRuntime(1996): ... 14 more
这是引用 MainActivity 类中的 sendMessage(View view) 方法。最初它看起来像这样:
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
但是,这导致了相同的 ClassCastException 问题。在我的研究中,我偶然发现了这篇文章: http ://code.google.com/p/android/issues/detail?id=32941
这表明我应该用 TextView 对象更改 EditText 对象。我这样做了。所以现在我的方法看起来像这样:
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
//EditText editText = (EditText) findViewById(R.id.edit_message);
TextView tView = (TextView)findViewById(R.id.edit_message);
String message = tView.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
那编译得很好。但我仍然得到与堆栈跟踪日志中所述相同的 ClassCastException 。我不知道如何解决这个问题。请指教。
艾伦