71019


Android
Studioでクイズアプリを作っているのですが、実機テストで「繰り返し停止しています」と表示されてしまいます。ググったところ、この`ArrayIndexOutOfBounds`が原因っぽく、quiz.txtのタブ文字入力を変えたり、配列の引数を確認しましたが、上手くいきません。  
大変申し訳ありませんが、お力添えお願い致します。

**quiz.txt**

[code]

    What is A?  A0  A1  A2
    What is B?  B0  B1  B2
    What is C?  C0  C1  C2
    
[/code]

**MyQuiz.java**

[code]

    package com.example.myquizapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class MyQuiz extends AppCompatActivity {
    
        private ArrayList<String[]> quizSet = new ArrayList<String[]>();
    
        private TextView scoreText;
        private TextView qText;
        private Button a0Button;
        private Button a1Button;
        private Button a2Button;
        private Button nextButton;
    
        private int currentQuiz = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_quiz);
    
            loadQuizSet();
    
            getViews();
    
            setQuiz();
        }
    
        private void setQuiz(){
            qText.setText(quizSet.get(currentQuiz)[0]);
    
            ArrayList<String> answers;
            answers = new ArrayList<>();
            for (int i = 1; i <= 2; i++){
                answers.add(quizSet.get(currentQuiz)[i]);
            }
            Collections.shuffle(answers);
    
            a0Button.setText(answers.get(0));
            a1Button.setText(answers.get(1));
            a2Button.setText(answers.get(2));
    
            nextButton.setEnabled(false);
        }
    
        public void checkAnswer(View view){
            //answer
            Button clickedButton = (Button) view;
            String clickedAnswer = clickedButton.getText().toString();
    
            //judge
            if (clickedAnswer.equals(quizSet.get(currentQuiz)[1])){
                clickedButton.setText("〇" + clickedAnswer);
            } else {
                clickedButton.setText(("×" + clickedAnswer));
            }
    
            //button
            a0Button.setEnabled(false);
            a1Button.setEnabled(false);
            a2Button.setEnabled(false);
            nextButton.setEnabled(true);
    
            //next quiz
            currentQuiz++;
        }
    
        private void getViews(){
            scoreText = (TextView) findViewById(R.id.scoreText);
            qText = (TextView) findViewById(R.id.qText);
            a0Button = (Button) findViewById(R.id.a0Button);
            a1Button = (Button) findViewById(R.id.a1Button);
            a2Button = (Button) findViewById(R.id.a2Button);
            nextButton = (Button) findViewById(R.id.nextButton);
        }
    
        private void loadQuizSet(){
            InputStream inputStream = null;
            BufferedReader bufferedReader = null;
            try{
                inputStream = getAssets().open("quiz.txt");
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String s;
                while ((s = bufferedReader.readLine()) != null){
                    quizSet.add(s.split("\t"));
                }
            } catch(IOException e) {
                e.printStackTrace();
            } finally {
                try{
                    if (inputStream != null) inputStream.close();
                    if (bufferedReader != null) bufferedReader.close();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
    
[/code]

**Logcat**

[code]

    2020-10-05 23:24:09.026 8966-8966/? E/ample.myquizap: Unknown bits set in runtime_flags: 0x8000
    2020-10-05 23:24:12.330 8966-8966/com.example.myquizapp E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.myquizapp, PID: 8966
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myquizapp/com.example.myquizapp.MyQuiz}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:214)
            at android.app.ActivityThread.main(ActivityThread.java:7356)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
         Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
            at com.example.myquizapp.MyQuiz.setQuiz(MyQuiz.java:48)
            at com.example.myquizapp.MyQuiz.onCreate(MyQuiz.java:39)
            at android.app.Activity.performCreate(Activity.java:7802)
            at android.app.Activity.performCreate(Activity.java:7791)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
            at android.os.Handler.dispatchMessage(Handler.java:107) 
            at android.os.Looper.loop(Looper.java:214) 
            at android.app.ActivityThread.main(ActivityThread.java:7356) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
    
[/code]


CC BY-SA 4.0
2020-10-05T14:28:57.000
70937
2020-10-08T13:10:28.023
2020-10-05T16:30:45.877
3060
42195
question
java android
Android アプリで「繰り返し停止しています」の原因は？


解決できました。 txtファイルだったので、「java」のタブ設定ではなく、「その他ファイル」のタブ文字設定にチェックを入れたら、エラーが解除されました。


CC BY-SA 4.0
2020-10-08T13:10:28.023
71019
2020-10-08T13:10:28.023
42195
70937
answer