查询sqlite数据库时AsyncTask中的RuntimeException

时间:2013-07-24 11:56:15

标签: android android-asynctask android-sqlite

我一直在尝试使用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):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:273)

     

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

     

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

     

07-24 16:06:20.477:E / AndroidRuntime(950):at 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):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)

     

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

     

07-24 16:06:20.477:E / AndroidRuntime(950):at 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):在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):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305)

     

07-24 16:06:20.477:E / AndroidRuntime(950):... 5更多

的AsyncTask

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;
}

为什么线程退出?我可以做些什么改变来使这段代码顺利运行?

1 个答案:

答案 0 :(得分: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;
};