程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android asynctask-SimpleCursorAdapter錯誤

android asynctask-SimpleCursorAdapter錯誤

編輯:編程綜合問答
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)

最佳回答:


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved