Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从db类中获取列表,并存储在其他类中_Java_Android_List_Get_Return - Fatal编程技术网

Java 从db类中获取列表,并存储在其他类中

Java 从db类中获取列表,并存储在其他类中,java,android,list,get,return,Java,Android,List,Get,Return,我正在我的db类SQLDatabaseHelper.java中创建一系列问题,并且有一个方法可以创建一个ListArray以在其他类中使用: public List<List<String>> getAllAnswersByQuestion2() { List<String> array1 = new ArrayList<String>(); List<String> array2 = new ArrayList

我正在我的db类SQLDatabaseHelper.java中创建一系列问题,并且有一个方法可以创建一个ListArray以在其他类中使用:

public List<List<String>> getAllAnswersByQuestion2() {
    List<String> array1 = new ArrayList<String>();    
    List<String> array2 = new ArrayList<String>();  

    //Clear arrays just in case they still existed and had values
    array1.clear();
    array2.clear();

    SQLiteDatabase db = this.getReadableDatabase();

   //Get 5 random values from the questions table
    String selectQuery = "SELECT DISTINCT questionDescr, answerSetId FROM " + TABLE_QUESTION + " ORDER BY RANDOM() LIMIT 5";

   Cursor c = db.rawQuery(selectQuery, null);   

   try {

       //Ignore any null values in the table, go the first value
    if (c!= null && c.moveToFirst())

   {
       do {

     //Create two strings  - one for all question descriptions and one for answersetIDs
     String questdescr = c.getString(c.getColumnIndex(QDESCR));
     String answerSetIds = c.getString(c.getColumnIndex(ASID)); 

     //Put these strings into two separate arrays
     array1.add(questdescr);
     array2.add(answerSetIds);

     //And when all values have been put into arrays......
    } while (c.moveToNext());

 }

    //Create another array to bundle up the two arrays already created
   List< List<String> > listArray = new ArrayList< List<String> >();

   //Clear this array just in case of residual values
   listArray.clear();

   listArray.add(array1);
   listArray.add(array2);

   //Return this array to be picked up in the 'showRandomNextQuestion' method
   return listArray;

} finally {

    //Close cursor and database to prevent memory leaks
        c.close();            
        db.close();
        }
}
当我的QuizQuestionBank类试图从db helper类检索listarray时,它似乎遇到了最初的问题。有人知道我需要做些什么来解决这个问题吗?非常感谢

在您的onPostExecute中尝试以下内容:

改变这个

questionView.setText(aQuestionString); 


包括完整的mainActivity代码。另外,SQLDatabaseHelper.java中的第445行是哪一行?第445行'SQLiteDatabase db=this.getReadableDatabase;'那么SQLDatabaseHelper扩展了SQLLiteDatabase?或者您是否编写了自己的getReadableDatabase方法?SQLDatabaseHelper扩展了SQLiteOpenHelper
private class showNextRandomQuestion1 extends AsyncTask<Void, Void, String> {

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

                    QuizQuestionBank qb = new QuizQuestionBank();

                       //question Strings
                       List<String> questionStrings = qb.getQuestionStrings();

                        //Generate random index
                        Random r = new Random();
                        int rand = Math.abs((r.nextInt() % questionStrings.size()));

                        String questionString = questionStrings.get(rand);  

                        //remove and square brackets from array of strings
                        String regex = "\\[|\\]";
                        aQuestionString = questionString.replaceAll(regex, "");
                        return aQuestionString; 
                }

                @Override
                protected void onPostExecute(String result) {
                    //take returned question from above and display it
                     questionView.setText(aQuestionString); 
                }

            }
08-31 19:24:55.637: I/Choreographer(20638): Skipped 63 frames!  The application may be doing too much work on its main thread.
08-31 19:24:55.887: I/ActivityManager(20638): Timeline: Activity_idle id: android.os.BinderProxy@41ec2f08 time:939408562
08-31 19:25:01.887: W/dalvikvm(20638): threadid=13: thread exiting with uncaught exception (group=0x41a71d88)
08-31 19:25:02.037: E/AndroidRuntime(20638): FATAL EXCEPTION: AsyncTask #2
08-31 19:25:02.037: E/AndroidRuntime(20638): Process: com.example.quizapp, PID: 20638
08-31 19:25:02.037: E/AndroidRuntime(20638): java.lang.RuntimeException: An error occured while executing doInBackground()
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.lang.Thread.run(Thread.java:841)
08-31 19:25:02.037: E/AndroidRuntime(20638): Caused by: java.lang.NullPointerException
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:263)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.SQLDatabaseHelper.getAllAnswersByQuestion2(SQLDatabaseHelper.java:445)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.QuizQuestionBank.getQuestionStrings(QuizQuestionBank.java:20)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.MainActivity$showNextRandomQuestion1.doInBackground(MainActivity.java:371)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at com.example.quizapp.MainActivity$showNextRandomQuestion1.doInBackground(MainActivity.java:1)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-31 19:25:02.037: E/AndroidRuntime(20638):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-31 19:25:02.037: E/AndroidRuntime(20638):    ... 4 more
08-31 19:25:02.227: W/dalvikvm(20638): threadid=14: thread exiting with uncaught exception (group=0x41a71d88)
08-31 19:25:02.227: I/Process(20638): Sending signal. PID: 20638 SIG: 9
questionView.setText(aQuestionString); 
questionView.setText(result);