我正在制作一个简单的表单,我想使用存储在我的 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 说我的错误发生在哪一行,尽管我没有。有没有办法启用它?
谢谢。