将我的应用程序安装到其他设备后,为什么会出现IndexOutOfBoundsException?

时间:2016-04-26 01:56:06

标签: java android sqlite indexoutofboundsexception

为什么我会收到此错误?

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)                                                                                   
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.muhammad.exquizme.PlayQuizActivity.displayQuestion(PlayQuizActivity.java:109)
at com.example.muhammad.exquizme.PlayQuizActivity.onCreate(PlayQuizActivity.java:62)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
 at android.app.ActivityThread.access$800(ActivityThread.java:178) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5637) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

此错误已在我的手机中出现,并且已经修复。然后我尝试在我的平板电脑设备中安装我的应用程序并运行它。当我按下按钮导航到PlayQuizActivity时,它会崩溃并且表示它为Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0

这是 PlayQuizActivity.java

的代码

公共类PlayQuizActivity扩展了AppCompatActivity {

private static final String QUESTION_MANAGER_URL = "http://mbcatubig.net16.net/ExQuiz%20Me/Quiz_Manager.php";
private int randomIndex, questionIndex = 1;
private SQLiteDatabaseHelper questionDatabase;
private RadioButton choiceBtnA, choiceBtnB, choiceBtnC, choiceBtnD;
private TextView questionTextView, qIndexView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_quiz);
    //questionDatabase = new SQLiteDatabaseHelper(this);

    qIndexView = (TextView) findViewById(R.id.currentNumber);
    questionTextView = (TextView) findViewById(R.id.questionTextView);
    choiceBtnA = (RadioButton) findViewById(R.id.choiceA);
    choiceBtnB = (RadioButton) findViewById(R.id.choiceB);
    choiceBtnC = (RadioButton) findViewById(R.id.choiceC);
    choiceBtnD = (RadioButton) findViewById(R.id.choiceD);

    //Get Network State
    ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isConnected = false;
    if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            isConnected = true;
        }
    }

    if (isConnected) {
        getJSONFromURL();
        displayQuestion();
    } else {
        displayQuestion();
    }


    choiceBtnA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswers(0);
        }
    });

    choiceBtnB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswers(1);
        }
    });

    choiceBtnC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswers(2);
        }
    });

    choiceBtnD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswers(3);
        }
    });
}

private void displayQuestion() {

    qIndexView.setText("QUESTION " + questionIndex++ + " /10");
    randomIndex = new Random().nextInt(10);
    questionDatabase = new SQLiteDatabaseHelper(this);

    //Uncheck button
    choiceBtnA.setChecked(false);
    choiceBtnB.setChecked(false);
    choiceBtnC.setChecked(false);
    choiceBtnD.setChecked(false);
    if (questionIndex <= 10) {
        String question = questionDatabase.getQuestions().get(randomIndex).question; //this causes the error
        String[] choices = new String[4];
        choices[0] = questionDatabase.getQuestions().get(randomIndex).choices[0];
        choices[1] = questionDatabase.getQuestions().get(randomIndex).choices[1];
        choices[2] = questionDatabase.getQuestions().get(randomIndex).choices[2];
        choices[3] = questionDatabase.getQuestions().get(randomIndex).choices[3];
        questionTextView.setText(question);
        choiceBtnA.setText(choices[0]);
        choiceBtnB.setText(choices[1]);
        choiceBtnC.setText(choices[2]);
        choiceBtnD.setText(choices[3]);
    }
}

private void checkAnswers(int index) {
    String answer = questionDatabase.getQuestions().get(randomIndex).choices[index], correctAnswer = questionDatabase.getQuestions().get(randomIndex).answer;
    if (answer.equals(correctAnswer)) {
        Toast.makeText(PlayQuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                displayQuestion();
            }
        }, 2000);
        //Log.d("Is it correct?", "Correct");
    } else {
        Toast.makeText(PlayQuizActivity.this, "Incorrect", Toast.LENGTH_SHORT).show();
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                displayQuestion();
            }
        }, 2000);
        //Log.d("Is it correct?", "Incorrect");
    }
}

private void getJSONFromURL() {
    class QuestionExtractorTask extends AsyncTask<Void, Void, String> {
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(PlayQuizActivity.this, "Loading questions...", null, true, true);
        }

        @Override
        protected String doInBackground(Void... params) {

            BufferedReader bufferedReader;
            try {
                URL url = new URL(QUESTION_MANAGER_URL);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String json;
                while ((json = bufferedReader.readLine()) != null) {
                    sb.append(json + "\n");
                }
                json = sb.toString().trim();

                return json;

            } catch (Exception e) {
                return null;
            }

        }

        @Override
        protected void onPostExecute(String jsonData) {
            super.onPostExecute(jsonData);
            loading.dismiss();
            QuestionJSONParser questionJsonParser = new QuestionJSONParser();
            questionJsonParser.extractJSON(jsonData);
        }
    }
    QuestionExtractorTask questionExtractorTask = new QuestionExtractorTask();
    questionExtractorTask.execute();
}


class QuestionJSONParser {
    private static final String ROOT_OBJECT = "questions", QUESTION = "question", CHOICES = "choices", CATEGORY = "category";

    public void extractJSON(String jsonData) {
        try {
            //Get the questions object
            JSONObject questions = new JSONObject(jsonData).getJSONObject(ROOT_OBJECT);
            //Get the questions' objects
            Collections.shuffle(uniqueRandomNumber(questions.length()));
            for (int counter = 0; counter < 10; counter++) {
                String[] choices = new String[4];


                System.out.println(uniqueRandomNumber(questions.length()));

                JSONObject theQuestionObject = questions.getJSONObject(uniqueRandomNumber(questions.length()).get(counter) + "");
                String question = theQuestionObject.getString(QUESTION), category = theQuestionObject.getString(CATEGORY), correctAnswer = "";

                //Log.d("question", question);
                JSONArray theChoicesArray = theQuestionObject.getJSONArray(CHOICES);

                //Get choice object from the choices object
                for (int i = 0; i < theChoicesArray.length(); i++) {
                    JSONObject choiceObject = theChoicesArray.getJSONObject(i);
                    choices[i] = choiceObject.optString("choice");
                    if (choiceObject.getInt("is_correct_choice") == 1) {
                        correctAnswer = choices[i];
                    }
                }
                //add in SQLite Database
                questionDatabase = new SQLiteDatabaseHelper(getApplicationContext());
                questionDatabase.addQuestion(new QuizQuestion(question, choices, correctAnswer, category));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public List uniqueRandomNumber(int length) {

        if (length != 0) {
            Integer[] arr = new Integer[length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = i + 1;
            }
            return Arrays.asList(arr);
        }
        return null;
    }

}

}

我想知道如何解决这个问题。我曾想过会导致错误的一些可能性。我想当从URL中提取json然后解析它时,也许它不会将这些数据保存到我的sqlite数据库,但是这个问题已经修复,所以我不知道这是怎么回事。它总是说大小是0.这是否意味着我需要坚持使用我的手机。我认为这不是关于sdk版本,不是吗?

这是我的 SQLiteDatabaseHelper .class:

class SQLiteDatabaseHelper扩展了SQLiteOpenHelper {

private static final String[] TABLES = new String[]{"question", "question_choices", "stats"};

public SQLiteDatabaseHelper(Context context) {
    super(context, "QUESTION_DATABASE", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    //Create Questions Table
    String CREATE_QUESTIONS_TABLE = "CREATE TABLE " + TABLES[0] + " (\n" +
            "  quiz_question_id INT  PRIMARY KEY,\n" +
            "  question TEXT ,\n" +
            "  choice_a TEXT,\n" +
            "  choice_b TEXT ,\n" +
            "  choice_c TEXT ,\n" +
            "  choice_d TEXT ,\n" +
            "  answer TEXT ,\n" +
            "   category TEXT)";
    db.execSQL(CREATE_QUESTIONS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLES[0]);
    db.execSQL("DROP TABLE IF EXISTS" + TABLES[2]);
}

//Managing question for the quiz
public void addQuestion(QuizQuestion questions) {
    SQLiteDatabase db = this.getWritableDatabase();

    //Insert in question table
    ContentValues values = new ContentValues();
    values.put("question", questions.question);
    values.put("choice_a", questions.choices[0]);
    values.put("choice_b", questions.choices[1]);
    values.put("choice_c", questions.choices[2]);
    values.put("choice_d", questions.choices[3]);
    values.put("answer", questions.answer);
    values.put("category", questions.category);

    // Inserting Row
    db.insert(TABLES[0], null, values);
    db.close(); // Closing database connection
}

//Managing best scores and best category for stats

public List<QuizQuestion> getQuestions() {
    List<QuizQuestion> quizQuestionArrayList = new ArrayList<>();
    String selectQuery = "SELECT * FROM " + TABLES[0];
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (!cursor.isLast()) {
        while (cursor.moveToNext()) {
            //Log.d("cursor.getString(1)", cursor.getString(1));
            String[] choices = {cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5)};
            quizQuestionArrayList.add(new QuizQuestion(cursor.getString(1), choices, cursor.getString(6), cursor.getString(7)));
        }
    }

    db.close();
    return quizQuestionArrayList;
}

}

上面的代码处理sqlite数据库。如果我连接到互联网,我基本上要在这里完成的工作,它会将数据下载到我的sqlite数据库,因此当它离线时,可以访问它。这种情况已经发生在我的手机上但是我的平板电脑没有按照上面的错误显示的那样工作。如果您需要更多详细信息,请告诉我们。任何帮助表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:2)

在我看来,你有一个空的数据库。您的db帮助程序的onCreate会创建一个表,但它永远不会向其中添加任何内容。

您的网络代码也不正确 - 您致电

    getJSONFromURL();
    displayQuestion();

但getJsonFromUrl启动异步任务。在任务完成之前,您不希望调用displayQuestion。 displayQuestion只应在onPostExecute中调用,或者如果数据是在过去下载的。

答案 1 :(得分:1)

由于代码适用于您的手机而非平板电脑, 最合理的一个不在于编码。

考虑一下您的数据库设置和连接,而不是编码问题 那是一个本地数据库吗?什么&#39;本地&#39;意思是,这是一个只在手机上可用的数据库吗?您是否真的为其他设备设置了数据库服务器,例如使用本地网络创建TOMCAT / Wamp服务器。 (在这种情况下,在PC上设置SQLite服务器并将其打开到您的设备。)

知道问题出在哪里并知道你在做什么是最重要的,因为你已经知道代码在手机上工作但平板电脑没有意味着至少已经出现问题,因为数据库在数据库之后什么也没有返回访问。