我正在做一个简单的表单,我想使用存储在我的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;
}
}
-从strings.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在哪里说我的错误发生在哪行,尽管我的没有。有没有办法启用它?
谢谢。
最佳答案
Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton
看这里。错误不是来自字符串资源。检查您的xml。您正在尝试将
Button
转换为RadioButton
。请更正。