如何使AsyncTask“doInBackground()”等待一个arraylist

时间:2017-08-07 13:56:14

标签: java android sql arraylist android-asynctask

我想在我的Android设备上保存本地SQL DB中的一些对象。 如果我尝试运行该应用程序,则在onPostExecute()中的单元格ArrayList上出现null object reference错误。

公共类CellApiTask扩展了AsyncTask> {     私有上下文上下文;

public CellApiTask(Context context) {
    this.context = context;
}

@Override
protected ArrayList<Cell> doInBackground(Void... params) {
    Fragment_Battery_Cell_Voltage cellVoltageClass = new Fragment_Battery_Cell_Voltage();
    return cellVoltageClass.getCellsForDB();
}


@Override
protected void onPostExecute(ArrayList<Cell> cell) {
    CellDbHelper dbHelper = new CellDbHelper(context);
    try{
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();

        try {
            /* delete old db if it exists */
            db.delete(CellEntryContract.CellEntry.TABLE_NAME_CELL, null, null);
        } catch (Exception e){
        }


        for (int i = 0; i<cell.size();i++) {

            values.put(CellEntryContract.CellEntry.COLUMN_NAME_CELL_NUMBER, cell.get(i).getCellNo());
            values.put(CellEntryContract.CellEntry.COLUMN_NAME_CELL_DENSITY, cell.get(i).getDensity());
            values.put(CellEntryContract.CellEntry.COLUMN_NAME_CELL_VOLTAGE, cell.get(i).getCellVoltage());
            db.insert(CellEntryContract.CellEntry.TABLE_NAME_CELL, null, values);
        }
        Log.i(CellStorage.class.toString(),"Cells successful saved");
    }catch(Exception ex) {
        Log.e(CellStorage.class.toString(), "Could not save cells in local data storage. ", ex);
    } finally {
        if (dbHelper != null)
            dbHelper.close();

    }
}

片段:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
....
    amountOfCells = (((AnnualServiceActivity) getActivity()).getBatteryFromIntent().getCell().intValue());
    generateGrid();
    CellStorage cellStorage = new CellStorage(getContext());
    cellStorage.runCellApiTasks();
    return view;
}

....
public ArrayList<Cell> getCellsForDB() {
    for (int i = 0; i < amountOfCells; i++) {
        Cell cell = new Cell();
        cell.setCellNo(i + 1);
        cell.setCellVoltage(0);
        cell.setDensity(0);
        Log.i("öööööööööööö",cellsForDB.get(i).getCellNo()+"");
        cellsForDB.add(cell);
    }
    return cellsForDB;
}
...

我认为doInBackground(...)方法已经完成,因为ArrayList在getCellsForDB()中已经准备就绪,因为我得到了空对象引用错误。那么是否可以让方法等到getCellsForDB()的ArrayList准备就绪?

崩溃日志:

    08-07 15:43:54.388 1585-1585/frontend.datastorage.CellStorage: Could not save cells in local data storage. 
                                                                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                                                                                                                                  at CellApiTask.onPostExecute(CellApiTask.java:49)
                                                                                                                                  at CellApiTask.onPostExecute(CellApiTask.java:20)
                                                                                                                                  at android.os.AsyncTask.finish(AsyncTask.java:660)
                                                                                                                                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                                                                                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                                                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
08-07 15:43:54.392 1585-1585/fronius.com.serviceapp_frontend E/class fronius.com.serviceapp_frontend.datastorage.CellStorage: Could not save cells in local data storage. 
                                                                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                                                                                                                                  at CellApiTask.onPostExecute(CellApiTask.java:49)
                                                                                                                                  at CellApiTask.onPostExecute(CellApiTask.java:20)
                                                                                                                                  at android.os.AsyncTask.finish(AsyncTask.java:660)
                                                                                                                                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                                                                                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                                                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

1 个答案:

答案 0 :(得分:0)

你不必在这里等待任何事情,当cellVoltageClass.getCellsForDB()返回时,doInBackground方法完成它的执行。 getCellsForDB()似乎返回null,因为cellsForDB为空(之前你没有得到崩溃的唯一原因是因为amountOfCells为0而forCellsForDB()内部没有环)