我一直在試用一個應用程序,我使用AsyncTask從sqlite表中檢索數據。偶爾會有一些問題沒有發生。但是,當它出現故障時,logcat的看起來像這樣在查詢sqlite數據庫時AsyncTask中的RuntimeException
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):at android.os.AsyncTask $ 3。完成(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):at java.util .concurrent.FutureTask.run(FutureTask.java:137)
07-24 16:06:20.477:E/AndroidRuntime(950):at 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):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):a噸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個
的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;
}
爲什麼線程退出?我可以做些什麼改變來讓代碼順利運行?
我覺得Exception是自我解釋的。您在PilgrimDao.java文件的第21行中得到空指針異常,所以在代碼中是第21行? –
'Cursor cursor = database.query(TABLE,COLUMNS,selection,params,null,null,null);' 這是行21. –
請確保數據庫和參數不爲NULL –