执行某些代码后,我在android studio中的应用崩溃

时间:2019-06-21 15:10:33

标签: java android crash

当我启动其他活动而不是主要活动时,应用程序崩溃。

我试图删除活动“问题”上的部分代码,以找出导致该问题的代码部分。

我对stackoverflow和android studio完全陌生,并使用javascript编码。我正在尝试制作此应用程序,并在问题活动中找到了所有代码。我无法编写自己的代码,但是我想通过谷歌搜索来学习它。

该应用程序具有可单击的文本视图,该视图有效。但是,当我添加从此处获取的代码时,它不起作用:Display multiple choice questions in a random order

这是崩溃的活动代码:

package com.example.myapplicationjava;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class questions extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);


        Scanner input = new Scanner(System.in);


        class Question {
            /*Read about private access specifier and getter/setter methods*/
            private String question;
            private String correctOption;
            private Question(String question, String correctOption) {
                this.question = question;
                this.correctOption = correctOption;
            }
        }


        List<Question> allQuestions = new ArrayList<>();
        allQuestions.add(new Question("YOUR_QUESTION", "CORRECT_OPTION"));
        /*Example:*/
        allQuestions.add(new Question("question \n A. answer1 \n B. answer2 \n C. answer3", "B"));
        /* TODO do this for all the 15 questions*/

        Collections.shuffle(allQuestions);

        //TODO prepare allQuestions as explained above
        int correct = 0;
        for (int i = 0; i < allQuestions.size(); i++) {
            Question curQuest = allQuestions.get(i);
            System.out.println(curQuest.question);
            String ans = input.next();
            if(ans.equalsIgnoreCase(curQuest.correctOption)) {
                System.out.println("Correct");
                correct++;
            } else {
                System.out.println("incorrect");
            }
        }
        System.out.println("You got "+ correct++ +"/15 good or bad job i guess");

    }
}

当这部分代码未注释时,启动此活动会使应用程序崩溃:

//TODO prepare allQuestions as explained above
        int correct = 0;
        for (int i = 0; i < allQuestions.size(); i++) {
            Question curQuest = allQuestions.get(i);
            System.out.println(curQuest.question);
            String ans = input.next();
            if(ans.equalsIgnoreCase(curQuest.correctOption)) {
                System.out.println("Correct");
                correct++;
            } else {
                System.out.println("incorrect");
            }
        }
        System.out.println("You got "+ correct++ +"/15 good or bad job i guess");

我希望该应用程序写入终端选项卡?,但为空。我还希望该应用程序不会崩溃。另一则有关应用程序崩溃的帖子要求提供logcat日志。在这里:

2019-06-21 17:04:51.426 1634-1645/? I/ActivityManager: START u0 {cmp=com.example.myapplicationjava/.questions} from uid 10080 on display 0

    --------- beginning of main
2019-06-21 17:04:51.435 1390-1412/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 7105255 , only wrote 7104960
2019-06-21 17:04:51.469 13313-13313/com.example.myapplicationjava I/System.out: YOUR_QUESTION
2019-06-21 17:04:51.470 13313-13313/com.example.myapplicationjava D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-06-21 17:04:51.470 13313-13313/com.example.myapplicationjava E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplicationjava, PID: 13313
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplicationjava/com.example.myapplicationjava.questions}: java.util.NoSuchElementException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Scanner.java:874)
        at java.util.Scanner.next(Scanner.java:1388)
        at com.example.myapplicationjava.questions.onCreate(questions.java:47)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
2019-06-21 17:04:51.483 1634-1988/? W/ActivityManager:   Force finishing activity com.example.myapplicationjava/.questions
2019-06-21 17:04:51.528 1634-1988/? W/ActivityManager:   Force finishing activity com.example.myapplicationjava/.MainActivity
2019-06-21 17:04:51.613 1634-8411/? I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-21 17:04:51.613 1634-8411/? D/OpenGLRenderer: Swap behavior 1
2019-06-21 17:04:51.614 1634-8411/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-06-21 17:04:51.614 1634-8411/? D/OpenGLRenderer: Swap behavior 0
2019-06-21 17:04:51.621 1634-8411/? D/EGL_emulation: eglCreateContext: 0xa00d92c0: maj 2 min 0 rcv 2
2019-06-21 17:04:51.637 1634-8411/? D/EGL_emulation: eglMakeCurrent: 0xa00d92c0: ver 2 0 (tinfo 0x889fe540)
2019-06-21 17:04:51.645 1634-8411/? D/EGL_emulation: eglMakeCurrent: 0xa00d92c0: ver 2 0 (tinfo 0x889fe540)
2019-06-21 17:04:52.029 1634-1647/? W/ActivityManager: Activity pause timeout for ActivityRecord{7589e89 u0 com.example.myapplicationjava/.questions t37 f}
2019-06-21 17:04:52.180 2115-2267/? D/EGL_emulation: eglMakeCurrent: 0xa2b85300: ver 2 0 (tinfo 0xa2b83640)
2019-06-21 17:04:52.693 2115-2267/? W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
2019-06-21 17:04:54.662 1390-1413/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 7412695 , only wrote 7259760
2019-06-21 17:05:02.632 1634-1647/? W/ActivityManager: Activity destroy timeout for ActivityRecord{7589e89 u0 com.example.myapplicationjava/.questions t37 f}
2019-06-21 17:05:02.633 1634-1647/? W/ActivityManager: Activity destroy timeout for ActivityRecord{62c23a u0 com.example.myapplicationjava/.MainActivity t37 f}
2019-06-21 17:05:02.633 1634-1647/? I/WindowManager: Destroying surface Surface(name=com.example.myapplicationjava/com.example.myapplicationjava.MainActivity) called by com.android.server.wm.WindowStateAnimator.destroySurface:2016 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:882 com.android.server.wm.WindowState.removeLocked:1456 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2484 com.android.server.wm.WindowManagerService.removeWindowLocked:2442 com.android.server.wm.WindowManagerService.removeWindowLocked:2311 com.android.server.wm.AppWindowToken.removeAllWindows:530 com.android.server.wm.AppWindowToken.removeAppFromTaskLocked:326

编辑:检查input.next是否为null时,应用仍然崩溃:

//TODO prepare allQuestions as explained above
        int correct = 0;
        for (int i = 0; i < allQuestions.size(); i++) {
            Question curQuest = allQuestions.get(i);
            System.out.println(curQuest.question);
            String ans = input.next();
            if(ans != null && ans.equalsIgnoreCase(curQuest.correctOption)) {
                System.out.println("Correct");
                correct++;
            } else {
                System.out.println("incorrect");
            }
        }
        System.out.println("You got "+ correct++ +"/15 good or bad job i guess");

有时,当选择YOUR_QUESTION示例问题时,会显示旧日志。从随机播放中选择另一个问题时,这是日志:

2019-06-21 18:03:00.435 15047-15047/? I/art: Not late-enabling -Xcheck:jni (already on)
2019-06-21 18:03:00.435 15047-15047/? W/art: Unexpected CPU variant for X86 using defaults: x86
2019-06-21 18:03:00.669 15047-15047/com.example.myapplicationjava W/System: ClassLoader referenced unknown path: /data/app/com.example.myapplicationjava-1/lib/x86
2019-06-21 18:03:00.679 15047-15047/com.example.myapplicationjava I/InstantRun: starting instant run server: is main process
2019-06-21 18:03:00.767 15047-15047/com.example.myapplicationjava W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.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
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void com.example.myapplicationjava.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:16)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6662)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2599)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2707)
2019-06-21 18:03:00.816 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1460)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.os.Looper.loop() (Looper.java:154)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6077)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:866)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:756)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.myapplicationjava-1/base.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myapplicationjava-1/split_lib_slice_8_apk.apk", zip file "/data/app/co
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void com.example.myapplicationjava.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:16)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6662)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2599)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2707)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1460)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.os.Looper.loop() (Looper.java:154)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6077)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:866)
2019-06-21 18:03:00.817 15047-15047/com.example.myapplicationjava I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:756)
2019-06-21 18:03:01.235 15047-15064/com.example.myapplicationjava I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-21 18:03:01.235 15047-15064/com.example.myapplicationjava D/OpenGLRenderer: Swap behavior 1
2019-06-21 18:03:01.235 15047-15064/com.example.myapplicationjava W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-06-21 18:03:01.236 15047-15064/com.example.myapplicationjava D/OpenGLRenderer: Swap behavior 0
2019-06-21 18:03:01.255 15047-15064/com.example.myapplicationjava D/EGL_emulation: eglCreateContext: 0x9e3f39e0: maj 2 min 0 rcv 2
2019-06-21 18:03:01.306 15047-15064/com.example.myapplicationjava D/EGL_emulation: eglMakeCurrent: 0x9e3f39e0: ver 2 0 (tinfo 0x9e3909e0)
2019-06-21 18:03:01.333 15047-15047/com.example.myapplicationjava W/art: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
2019-06-21 18:03:01.342 15047-15064/com.example.myapplicationjava D/EGL_emulation: eglMakeCurrent: 0x9e3f39e0: ver 2 0 (tinfo 0x9e3909e0)
2019-06-21 18:03:08.480 15047-15047/com.example.myapplicationjava I/System.out: question 
2019-06-21 18:03:08.480 15047-15047/com.example.myapplicationjava I/System.out:  A. answer1 
2019-06-21 18:03:08.480 15047-15047/com.example.myapplicationjava I/System.out:  B. answer2 
2019-06-21 18:03:08.480 15047-15047/com.example.myapplicationjava I/System.out:  C. answer3
2019-06-21 18:03:08.481 15047-15047/com.example.myapplicationjava D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-06-21 18:03:08.481 15047-15047/com.example.myapplicationjava E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplicationjava, PID: 15047
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplicationjava/com.example.myapplicationjava.questions}: java.util.NoSuchElementException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Scanner.java:874)
        at java.util.Scanner.next(Scanner.java:1388)
        at com.example.myapplicationjava.questions.onCreate(questions.java:47)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

我可能会尝试删除整个system.next内容,并在完成更多谷歌搜索后尝试以其他方式查看问题。感谢到目前为止的所有建议。

2 个答案:

答案 0 :(得分:1)

您的问题是,您试图在Scanner对象上调用input.next(),而不检查它是否实际上还有其他元素,这就是为什么要获取NoSuchElementException的原因。请尝试以下方法:

    if(input.hasNext()) {
        String ans = input.next();
        if(ans.equalsIgnoreCase(curQuest.correctOption)) {
            System.out.println("Correct");
            correct++;
        } else {
            System.out.println("incorrect");
        }
    }

这不会使其崩溃,您可以在Logcat中看到日志,但是它仍然无法达到我想像的方式。

答案 1 :(得分:0)

使用string ans = input.next()似乎导致应用崩溃。我不再使用input.next。