1

我正在制作一个简单的表单,我想使用存储在我的 strings.xml 中的数组与用户输入的文本进行比较。

    Resources res = getResources();
    String[] usernameArray = res.getStringArray(R.array.usernames);
    int userint = usernameArray.length;
    for(int x=0;x<userint;x++){
        if(user.equals(usernameArray[x])){
            Toast.makeText(this, "Username already exists.", Toast.LENGTH_SHORT).show();
            return;
        }
    }

-从字符串.xml

<string-array name="usernames">
    <item>Bryan</item>
    <item>John</item>
    <item>Matt</item>
    <item>Mike</item>
</string-array>

当我的应用程序运行我的这部分代码时,我在 logcat 上收到很多错误:

09-11 00:25:59.725: E/AndroidRuntime(810): FATAL EXCEPTION: main
09-11 00:25:59.725: E/AndroidRuntime(810): java.lang.IllegalStateException: Could not execute method of the activity
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.view.View$1.onClick(View.java:3633)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.view.View.performClick(View.java:4240)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.view.View$PerformClick.run(View.java:17721)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.os.Handler.handleCallback(Handler.java:730)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.os.Looper.loop(Looper.java:137)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 00:25:59.725: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
09-11 00:25:59.725: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:525)
09-11 00:25:59.725: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 00:25:59.725: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 00:25:59.725: E/AndroidRuntime(810):  at dalvik.system.NativeStart.main(Native Method)
09-11 00:25:59.725: E/AndroidRuntime(810): Caused by: java.lang.reflect.InvocationTargetException
09-11 00:25:59.725: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
09-11 00:25:59.725: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:525)
09-11 00:25:59.725: E/AndroidRuntime(810):  at android.view.View$1.onClick(View.java:3628)
09-11 00:25:59.725: E/AndroidRuntime(810):  ... 11 more
09-11 00:25:59.725: E/AndroidRuntime(810): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton
09-11 00:25:59.725: E/AndroidRuntime(810):  at com.example.myapplication.MainActivity.hitClear(MainActivity.java:81)
09-11 00:25:59.725: E/AndroidRuntime(810):  ... 14 more

另外:当我问另一个问题时,我被问到 LogCat 说我的错误发生在哪一行,尽管我没有。有没有办法启用它?

谢谢。

4

3 回答 3

1

这是你真正的问题:

android.widget.Button 不能转换为 android.widget.RadioButton

于 2013-09-11T04:39:55.263 回答
1
Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton

看这里。错误不是来自字符串资源。检查你的 xml。您正在尝试转换ButtonRadioButton. 请改正。

于 2013-09-11T04:40:02.607 回答
1
09-11 00:25:59.725: E/AndroidRuntime(810): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton
09-11 00:25:59.725: E/AndroidRuntime(810):  at com.example.myapplication.MainActivity.hitClear(MainActivity.java:81)

检查 MainActivity.java,第 81 行。您可能有以下内容:

RadioButton button = (RadioButton) findById( R.id... );

其中R.id...指的是 a Button,而不是 a RadioButton

于 2013-09-11T04:42:30.200 回答