gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:37:07 27 4
gpt4 key购买 nike

为什么我会收到此错误?

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的代码:

公共(public)类 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:

类 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 数据库中,这样当它离线时,就可以访问它。我的手机上已经发生过这种情况,但在我的平板电脑上,它无法正常工作,如上面的错误所示。如果您需要更多详细信息,请告诉我。任何帮助表示赞赏。谢谢。

最佳答案

在我看来,你的数据库是空的。数据库助手的 onCreate 创建了一个表,但它从不向其中添加任何内容。

您的网络代码也不正确 - 您调用

    getJSONFromURL();
displayQuestion();

但是 getJsonFromUrl 启动了一个异步任务。您不希望在任务完成之前调用 displayQuestion。 displayQuestion 只能在 onPostExecute 中调用,或者在过去下载过数据的情况下调用。

关于java - 为什么将我的应用程序安装到其他设备后会出现 IndexOutOfBoundsException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com