這是第一次我會在教程如何使用以下URL創建Android上的應用程序:MyFirstApp拋出ClassCastException上MyFirstApp
當我跑了,我得到了我的堆棧跟蹤一個ClassCastException通過logcat的這看起來像這樣:
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視圖)方法。最初它看起來像這樣:
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);
}
編譯得很好。但是我仍然得到了與stacktrace日誌中所述的相同的ClassCastException。我不知道如何解決這個問題。請指教。
艾倫
也見[構建一個簡單的用戶界面] [起始另一個活動](HTTP之前(http://developer.android.com/training/basics/firstapp/building-ui.html)訓練部分://顯影劑。 android.com/training/basics/firstapp/starting-activity.html) – 2013-03-03 17:52:30