0

我一直在尝试使用 AsyncTask 从 sqlite 表中检索数据的应用程序。它偶尔会在没有那么多问题的情况下工作。但是当它失败时,Logcat 看起来像这样

07-24 16:06:20.477:E/AndroidRuntime(950):致命异常:AsyncTask #1

07-24 16:06:20.477: E/AndroidRuntime(950): java.lang.RuntimeException: 执行 doInBackground() 时出错

07-24 16:06:20.477: E/AndroidRuntime(950): 在 android.os.AsyncTask$3.done(AsyncTask.java:278)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.FutureTask.setException(FutureTask.java:124)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.FutureTask.run(FutureTask.java:137)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.lang.Thread.run(Thread.java:856)

07-24 16:06:20.477:E/AndroidRuntime(950):原因:java.lang.NullPointerException

07-24 16:06:20.477: E/AndroidRuntime(950): at com.brionsoftwares.android.pilgrimhelper.test.PilgrimDao.getPilgrim(PilgrimDao.java:21)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 com.brionsoftwares.android.pilgrimhelper.PilgrimSearchActivity$DataQuery.doInBackground(PilgrimSearchActivity.java:45)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 com.brionsoftwares.android.pilgrimhelper.PilgrimSearchActivity$DataQuery.doInBackground(PilgrimSearchActivity.java:1)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 android.os.AsyncTask$2.call(AsyncTask.java:264)

07-24 16:06:20.477: E/AndroidRuntime(950): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

07-24 16:06:20.477: E/AndroidRuntime(950): ... 还有 5 个

异步任务

protected Void doInBackground(String... params) {
        List<Pilgrim> pilgrimList=PilgrimDao.getPilgrim(pilgrimDatabase.getSqlLiteDatabase(), params);
        ArrayList<Map<String, String>> list = buildData(pilgrimList);
        String[] from={"Name","District"};
        int[] to={android.R.id.text1, android.R.id.text2};
        simpleAdapter= new SimpleAdapter(getApplicationContext(), list, R.layout.simple_list_item_2, from, to);
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        mProgressBar.setVisibility(View.GONE);
        mListView.setAdapter(simpleAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Toast.makeText(PilgrimSearchActivity.this, "Select", Toast.LENGTH_LONG).show();

            }

        });
        super.onPostExecute(result);
    }



}

PilgrimDao.getPilgrim 方法

public static List<Pilgrim> getPilgrim(SQLiteDatabase database, String[] params) {
    List<Pilgrim> shippers = new LinkedList<Pilgrim>();
    String selection="Name = ?";
    Cursor cursor = database.query(TABLE, COLUMNS, selection, params, null,null, null);          //line 21
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Pilgrim pilgrim= new Pilgrim();
        pilgrim.setName(cursor.getString(0));
        pilgrim.setDistrict(cursor.getString(1));
        shippers.add(pilgrim);
        cursor.moveToNext();
    }
    cursor.close();
    Log.v(PilgrimSearchActivity.TAG,"PilgrimDao returning " + shippers.size() + " shippers");
    return shippers;
}

为什么线程退出?我可以进行哪些更改以使此代码顺利运行?

4

1 回答 1

2

正如你在评论中看到的,你得到一个空指针异常,可能是你传递的 URI 是错误的,或者任何参数,确保是正确的。尝试这个:

public static List<Pilgrim> getPilgrim(SQLiteDatabase database, String[] params) {
 List<Pilgrim> shippers = new LinkedList<Pilgrim>();
 String selection="Name = ?";
 try{
   Cursor cursor = database.query(TABLE, COLUMNS, selection, params, null,null,null);          //line 21
   cursor.moveToFirst();
   while (!cursor.isAfterLast()) {
     Pilgrim pilgrim= new Pilgrim();
     pilgrim.setName(cursor.getString(0));
     pilgrim.setDistrict(cursor.getString(1));
     shippers.add(pilgrim);
     cursor.moveToNext();
    }
 }catch (Exception e)
 {
   e.printStackTrace();//this will print in your logcat any possible exception.
 }
 cursor.close();
 Log.v(PilgrimSearchActivity.TAG,"PilgrimDao returning " + shippers.size() + " shippers");
 return shippers;
};
于 2013-07-24T16:00:19.380 回答