【问题标题】:Android Quiz If-Else Statements and ToastsAndroid 测验 If-Else 语句和祝酒词
【发布时间】:2017-06-26 09:38:26
【问题描述】:

Java 代码是我目前在 Android Studio 中所拥有的,作为我正在开发的多项选择测验应用程序的一部分。

我想创建一个在单击按钮时显示的 toast。到目前为止,我有以下代码。根据是否单击了正确的按钮(即按钮字符串等于数组中相应元素的字符串),应显示不同的 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

【问题讨论】:

  • 请提供logcat
  • 在问题中发布您的错误日志
  • 我们打赌你得到了 NullPointerException 吗?
  • 我尝试添加 logcat。我希望我在上面的编辑中做了正确的事情。
  • @Rotwang 你能解释一下如何避免 NullPointerException,如果那是我得到的吗?我在网上查了文档,但不是很明白是什么意思(行话太多我不熟悉)。

标签: java android arrays if-statement android-toast


【解决方案1】:

非常感谢 Rotwang 以及所有试图提供帮助的人。我终于找到了我的错误,我更正的代码涉及将 submitAnswerA 方法设置为 submitAnswerA(View view) 并将 correctAnswer 和 buttonA 初始化为公共类中的全局变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2014-02-15
    • 2019-04-09
    相关资源
    最近更新 更多