java代码是我目前在Android Studio中的代码,是我正在开发的多项选择测验应用程序的一部分。
我想创建一个单击按钮时显示的Toast。截至目前,我有以下代码。根据是否单击了正确的按钮(即,按钮String等于数组中相应元素的String),应显示不同的toast。
我是一名新手开发人员(Java和Android Studio都是新手),代码看起来很好,但不幸的是,每当我尝试点击应用程序中的按钮时,我都会收到运行时错误,我的应用程序被强制执行关闭Android Studio中的调试也没有帮助我。
如果您对我犯了错误有任何建议,我会非常非常高兴。提前感谢您的帮助。
/**Array containing correct answers for user**/
String[] correctAnswerForUser = new String[3];
/**Initialising array elements of answers array*/
correctAnswerForUser[0] = "A";
correctAnswerForUser[1] = "C";
correctAnswerForUser[2] = "blank";
/**Loop through the correct answers**/
for (int index = 0; index < correctAnswerForUser.length; index++) {
String correctAnswer = correctAnswerForUser[index];
}
}
/**Method that stores the String "A" when Button A is clicked.**/
public void submitAnswerA(String correctAnswer) {
String buttonA = "A";
/**Displays toast when button is clicked**/
if (buttonA.matches(correctAnswer)){
Toast.makeText(getApplicationContext(), "Correct Answer",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "Incorrect Answer",Toast.LENGTH_LONG).show();
}
}
这是我在logcat中的输出:
06-26 12:29:07.135 21925-21925/android.com.example.buddinggeographers I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8e8d95d time:104007253
这是我的logcat的长版本:
06-26 14:04:23.193 24582-24582/? I/art: Late-enabling -Xcheck:jni
06-26 14:04:23.249 24582-24588/? I/art: Debugger is no longer active
06-26 14:04:23.352 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.535 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.586 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.630 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.696 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.755 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.802 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.868 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.916 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:23.964 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:24.031 24582-24582/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/android.com.example.buddinggeographers-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@android.com.example.buddinggeographers-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
06-26 14:04:24.034 24582-24582/? W/System: ClassLoader referenced unknown path: /data/app/android.com.example.buddinggeographers-1/lib/arm
06-26 14:04:24.038 24582-24582/? I/InstantRun: starting instant run server: is main process
06-26 14:04:24.148 24582-24582/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-26 14:04:24.335 24582-24728/android.com.example.buddinggeographers D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-26 14:04:24.404 24582-24728/android.com.example.buddinggeographers I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (I3f4bae6ca5)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 02/14/16 Sun
Local Branch: mybranch18261495
Remote Branch: quic/LA.BF.1.1.3_rb1.10
Local Patches: NONE
Reconstruct Branch: NOTHING
06-26 14:04:24.407 24582-24728/android.com.example.buddinggeographers I/OpenGLRenderer: Initialized EGL, version 1.4
06-26 14:04:24.470 24582-24582/android.com.example.buddinggeographers W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
06-26 14:04:24.576 24582-24582/android.com.example.buddinggeographers I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8e8d95d time:106252058
答案 0 :(得分:0)
非常感谢Rotwang和其他所有试图提供帮助的人。我终于发现了我的错误,我的更正代码涉及将submitAnswerA方法设置为submitAnswerA(View视图)并将correctAnswer和buttonA初始化为公共类中的全局变量。