我最近在AsyncTask中包装了一个主线程方法。它似乎认为我的代码中的某些东西仍在尝试访问UI线程。但我不确定它是什么 - 这是我的AsyncTask:
private class showNextRandomQuestion extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
SQLDatabaseHelper db = new SQLDatabaseHelper(MainActivity.this);
//get the data from the database
List<List<String>> listList = db.getAllAnswersByQuestion1();
//Get the question/text/answer Strings
List<String> questionStrings = listList.get(0); //question Strings
List<String> answerSetIds = listList.get(1);
//Generate random index
Random r = new Random();
int rand = Math.abs((r.nextInt() % questionStrings.size()));
//get answer description for randomly selected question
String questionString = questionStrings.get(rand);
String answerSetId = answerSetIds.get(rand);
String regex = "\\[|\\]";
answerId = answerSetId.replaceAll(regex, "");
mQuestionString = questionString.replaceAll(regex, "");
return mQuestionString;
}
@Override
protected void onPostExecute(String result) {
questionView.setText(mQuestionString);
}
}
这是我的logcat:
05-25 08:55:35.787: W/CursorWindow(20418): Window is full: requested allocation 132 bytes, free space 104 bytes, window size 2097152 bytes
05-25 08:55:39.437: W/dalvikvm(20418): threadid=12: thread exiting with uncaught exception (group=0x415aad88)
05-25 08:55:39.537: E/AndroidRuntime(20418): FATAL EXCEPTION: AsyncTask #2
05-25 08:55:39.537: E/AndroidRuntime(20418): Process: com.example.quizapp, PID: 20418
05-25 08:55:39.537: E/AndroidRuntime(20418): java.lang.RuntimeException: An error occured while executing doInBackground()
05-25 08:55:39.537: E/AndroidRuntime(20418): at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-25 08:55:39.537: E/AndroidRuntime(20418): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.lang.Thread.run(Thread.java:841)
05-25 08:55:39.537: E/AndroidRuntime(20418): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 383776
05-25 08:55:39.537: E/AndroidRuntime(20418): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
05-25 08:55:39.537: E/AndroidRuntime(20418): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-25 08:55:39.537: E/AndroidRuntime(20418): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-25 08:55:39.537: E/AndroidRuntime(20418): at com.example.quizapp.SQLDatabaseHelper.getAllAnswersByQuestion1(SQLDatabaseHelper.java:393)
05-25 08:55:39.537: E/AndroidRuntime(20418): at com.example.quizapp.MainActivity$showNextRandomQuestion.doInBackground(MainActivity.java:395)
05-25 08:55:39.537: E/AndroidRuntime(20418): at com.example.quizapp.MainActivity$showNextRandomQuestion.doInBackground(MainActivity.java:1)
05-25 08:55:39.537: E/AndroidRuntime(20418): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-25 08:55:39.537: E/AndroidRuntime(20418): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-25 08:55:39.537: E/AndroidRuntime(20418): ... 4 more
有人可以指出他们认为仍然可以访问UI线程的内容。非常感谢。
答案 0 :(得分:0)
我认为这是由于索引超出界限所以你正在访问那些不存在的东西。请查看第395行的内容。