MyFirstApp上的ClassCastException

时间:2013-03-03 17:45:47

标签: android

我第一次使用以下网址浏览有关如何在Android上创建应用的教程:MyFirstApp

当我运行它时,我通过LogCat在我的stacktrace上得到了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视图)方法。最初它看起来像这样:

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。我不知道如何解决这个问题。请指教。

艾伦

1 个答案:

答案 0 :(得分:1)

res/layout/activity_main.xml(这是默认布局文件,项目中的名称可能不同),edit_message的类型是什么?

它应该与你的演员(TextView)findViewById(R.id.edit_message);中的那个相同。