由于重新打开已关闭的对象,SQLite数据库崩溃

时间:2014-06-18 08:37:27

标签: android database sqlite

我有我的DatabaseHelper

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "name";
private static final int DATABASE_VERSION = 1;

private static DatabaseHelper dbHelper;

private Context mTempCtx = null;

private DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    mTempCtx = context;
}


public static DatabaseHelper getInstance(Context context) {
    if (null == dbHelper) {
        dbHelper = new DatabaseHelper(context);
    }
    return dbHelper;
} 
...

我使用标签的主要活动

import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {

// private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DatabaseHelper.getInstance(getApplicationContext())
            .getWritableDatabase();

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ...
第一个标签中的

和片段

import android.support.v4.app.Fragment;
public class CalendarFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ...
    Cursor cur = getActivity().getContentResolver().query(
            TABLE.CONTENT_URI, TABLE.PROJECTION, null, null, null);

此查询使我的

06-18 08:21:05.854: E/AndroidRuntime(1023): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: ...

此代码适用于4.2.2和4.3,4.4,但我在4.1和2.3.7上崩溃。现在,在阅读了一些教程和问题之后,它仍然无法为我工作。

1 个答案:

答案 0 :(得分:0)

如果你想在db.close之后做一些数据库操作,那么你必须调用db.getWritableDatabase()。