错误,bur无法关闭游标/数据库

时间:2014-06-27 21:21:11

标签: android database sqlite cursor android-sqlite

我一直收到错误" DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象",但我找不到游标/数据库已关闭。

有人可以告诉我忘记关闭的地方吗?

我正在使用自定义适配器:

OnCreate MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    gridview = (GridView) findViewById(R.id.gridview);

    db = new DatabaseHelper(this);
    db.open();
    mCursor = db.getAllRecords();

    MyCursorAdapter mAdapter = new MyCursorAdapter(MyActivity.this, mCursor, false);
    mAdapter.notifyDataSetChanged();

    gridview.setAdapter(mAdapter);
    mCursor.close();
    db.close();
}

MyCursorAdapter.java

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MyCursorAdapter extends CursorAdapter {

private final LayoutInflater mInflater;
private ContentResolver mContent;

public Context cContext ;

public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
    super(context, c, autoRequery);
    mInflater = LayoutInflater.from(context);
    mContent = context.getContentResolver();
}


ImageView iv;

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mInflater.inflate(R.layout.list_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    String path = cursor.getString(cursor.getColumnIndex("path"));
    iv = (ImageView) view.findViewById(R.id.imageView);
    new loadImage().execute(path);

    String userName = cursor.getString(cursor.getColumnIndex("title"));
    TextView userNameTV = (TextView) view.findViewById(R.id.textView);
    userNameTV.setText(userName);

    cursor.close();
}

private class loadImage extends AsyncTask<String, Integer, Bitmap> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        // example of setting up something
        dialog = new ProgressDialog(cContext);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.show();
    }

    @Override
    protected Bitmap doInBackground(String... strings) {

        for (int i = 0; i < 20; i++) {
            publishProgress(5);
            try {
                Thread.sleep(88);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        dialog.dismiss();

        FileInputStream fin;

        try {
            fin = cContext.openFileInput(strings[0]);
            if(fin !=null && fin.available() > 0) {
                return BitmapFactory.decodeStream(fin);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        iv.setImageBitmap(bitmap);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        dialog.incrementProgressBy(progress[0]);
    }
}
}

可以肯定的是,我在MainActivity的OnPause和OnDestroy中添加了我的附件,如下所示:

@Override
protected void onDestroy() {
    if (mCursor != null) {
        mCursor.close();
    }
    if (db != null) {
        db.close();
    }
}

@Override
protected void onPause() {
    if (mCursor != null) {
        mCursor.close();
    }
    if (db != null) {
        db.close();
    }
}

整个错误

06-27 16:29:52.238      879-888/com.android.email E/StrictMode﹕ Finalizing a Cursor that     has not been deactivated or closed. database =     /data/data/com.android.email/databases/EmailProvider.db, table = null, query = SELECT _id     AS _id,'SW5ib3g' AS persistentId,
'content://com.android.email.provider/uifolder/' || _id AS folderUri,displayName AS   name,flags&1 AS hasChildren,'16424' AS capabilities,3 AS syncWindow,'content://com.android.email.provider/uimessages/' || _id AS         conversationListUri,
'content://com.android.email.provider/uisubfolders/' || _id AS childFoldersListUri,NULL     AS     unseenCount,unreadCount AS unreadCount,CASE WHEN totalCount<0 OR type=3 OR type=4 OR      type=6     THEN messageCount ELSE totalCount END AS totalCount,
'content://com.android.email.provider/uirefresh/' || _id AS refreshUri,uiSyncStatus AS  syncStatus,uiLastSyncResult AS lastSyncResult,CASE type WHEN 0 THEN 2 WHEN 3 THEN 4 WHEN 4  THEN 8 WHEN 5 THEN 16 WHEN 6 THEN 32 WHEN 7 THEN 64 WHEN 9 THEN 128 WHEN 10 THEN 
2048 WHEN 8 THEN 4097 ELSE 1 END AS type,CASE type WHEN 0 THEN 2130837570 WHEN 3 THEN 2130837566 WHEN 4 THEN 2130837574 WHEN 5 THEN 2130837578 WHEN 6 THEN 2130837586 WHEN 9 THEN   2130837582 ELSE -1 END AS iconResId,NULL AS notific

android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
        at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:98)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:50)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
        at com.android.email.provider.EmailProvider.uiQuery(EmailProvider.java:4302)
        at com.android.email.provider.EmailProvider.query(EmailProvider.java:1212)
        at android.content.ContentProvider.query(ContentProvider.java:857)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:200)
        at android.content.ContentResolver.query(ContentResolver.java:461)
        at android.content.ContentResolver.query(ContentResolver.java:404)
        at com.android.email.NotificationController.refreshNotificationsForAccountInternal(NotificationController.java:635)
        at com.android.email.NotificationController.refreshNotificationsForAccount(NotificationController.java:588)
        at com.android.email.NotificationController.access$1200(NotificationController.java:65)
        at com.android.email.NotificationController$MessageContentObserver.onChange(NotificationController.java:705)
        at android.database.ContentObserver.onChange(ContentObserver.java:129)
        at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:180)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at com.android.email.NotificationController$NotificationThread.run(NotificationController.java:792)
        at java.lang.Thread.run(Thread.java:841)

0 个答案:

没有答案