2 yue1liang1chuan yue1liang1chuan 于 2013.06.07 15:16 提问

SimpleCursorAdapter错误

在异步任务中添加SimpleCursorAdapter 时出现错误。

private class LoadNewsLists extends AsyncTask<String, Void, Cursor> {
     @Override
     protected void onPreExecute() {         
     }


     @Override
     protected Cursor doInBackground(final String... args) {
         Cursor cursor = dbHelper.fetchMainActivity();
         return cursor;
     }


     protected void onPostExecute(final Cursor cursor) {
         String[] columns = new String[] {
                  DatabaseUtil.rtitle,
                  DatabaseUtil.rtypeof
          };

          // the XML defined views which the data will be bound to
          int[] to = new int[] {
            R.id.idTitle,
            R.id.idType
          };

          // create the adapter using the cursor pointing to the desired data
          //as well as the layout information
          dataAdapter = new SimpleCursorAdapter(
            MainActivity.this, R.layout.listhome,
            cursor,
            columns,
            to,
            0);

          listView = (ListView) findViewById(R.id.list);
          listView.setAdapter(dataAdapter);          
     }

然后出现这些错误:

06-07 09:14:33.496: E/AndroidRuntime(21735): Caused by: java.lang.NullPointerException
06-07 09:14:33.496: E/AndroidRuntime(21735):    
at com.ppshein.test.MainActivity$LoadNewsLists.doInBackground(MainActivity.java:38)

3个回答

shaojie519
shaojie519   2013.06.07 16:54
已采纳

检查下在你返回dbHelper.fetchMainActivity()之前是否调用了cursor.close()方法,或者你取到的cursor就是null

Jave_
Jave_   2013.06.07 15:21

错误很明显 空指针,Cursor cursor = dbHelper.fetchMainActivity();做了什么操作

ren_list
ren_list   2013.06.07 15:36

这报的是空指针异常,好好看看那个地方传值是空的

Csdn user default icon
上传中...
上传图片
插入图片