AsyncTask中的android SimpleCursorAdapter发现错误

时间:2013-06-07 02:45:55

标签: android android-asynctask simplecursoradapter

当我尝试在AsyncTask中添加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)

1 个答案:

答案 0 :(得分:0)

检查dbHelper实例。它可能是null,意味着这一行将通过NPE:

Cursor cursor = dbHelper.fetchMainActivity();