我正在创建一个简单的表单,我想使用存储在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;
}
}
-from 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说我的错误发生在哪一行,尽管我没有。有没有办法启用它?
感谢。
答案 0 :(得分:1)
这是你真正的问题:
android.widget.Button无法强制转换为android.widget.RadioButton
答案 1 :(得分:1)
Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton
见这里。错误不是来自String资源。检查你的xml。您正在尝试将Button
转换为RadioButton
。请更正。
答案 2 :(得分: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...
指的是Button
,而不是RadioButton
。