-2

The java code is what I have so far in Android Studio, as part of a multiple choice quiz app I am developing.

I want to create a toast that is shown when a button is clicked. As of now I have the following code. Depending on whether the correct button is clicked (ie. the button String is equal to the String of the respective element in the array) a different toast should be displayed.

I am a novice developer (new to both Java and Android Studio), and the code looks fine to me, but unfortunately, whenever I try to click the button in the app, I get a runtime error and my app is forced to close. Debugging in Android Studio hasn't helped me either.

If you have any suggestions where I made a mistake, I would be very, very happy. Thank you in advance for your help.

    /**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();
        }

}

This is my output in the 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

This is the long version of my 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
Royz
  • 145
  • 1
  • 15
astonky
  • 1
  • 2
  • 2
    please provide logcat – hamid_c Jun 26 '17 at 09:40
  • 1
    post your error log here in the question – Rahul Khurana Jun 26 '17 at 09:41
  • Let's bet you are getting a NullPointerException? – Phantômaxx Jun 26 '17 at 09:59
  • I tried to add the logcat. I hope I did the correct thing in the edits above. – astonky Jun 26 '17 at 10:28
  • @Rotwang Can you explain how to avoid a NullPointerException, if that is what I am getting? I looked up the documentation online, but I don't really understand what it means (there's too much jargon that I'm not familiar with). – astonky Jun 26 '17 at 10:30
  • NPEs are due to an object which is used **before** (or **if**) being instanced. The soulutionis to find that object and make sure it's being instanced **before** using it. – Phantômaxx Jun 26 '17 at 10:41
  • @Rotwang I am not quite sure I understand what you mean. Are you saying that I might be using an object for the first time in an if statement (or another type of statement), whereas I should have initialised that object before the if-statement (or before its first use)? If that's not what you mean, could you please give an example? – astonky Jun 26 '17 at 10:45
  • Yes, before its first usage. And it must be visible to the current scope. – Phantômaxx Jun 26 '17 at 11:16
  • @Rotwang Ok. Thank you very much for the clarification. I've added the logcat above (I hope I've done it correctly), but I still don't seem to find my mistake. Is there anything that I'm doing wrong with the logcat? – astonky Jun 26 '17 at 11:42
  • We need the **full** logcat (I know, it's long). Especially the part which says `Caused by ...` – Phantômaxx Jun 26 '17 at 11:50
  • @Rotwang Thanks. I've added the full logcat (I hope this is it). I think that there is some sort of runtime error, but I would be happy if you could clarify this to me. – astonky Jun 26 '17 at 12:02
  • No, these are just system messages. Not the error logs. – Phantômaxx Jun 26 '17 at 14:07
  • @Rotwang O sorry. Could you explain where I can find the error logs? – astonky Jun 26 '17 at 17:12
  • In the logcat... – Phantômaxx Jun 27 '17 at 08:43

1 Answers1

0

Thank you very much Rotwang, and everyone else who tried to help. I've finally found my error, and my corrected code involved making the submitAnswerA method to be submitAnswerA(View view) and initialising correctAnswer and buttonA as global variables in the public class.

astonky
  • 1
  • 2