android SQLite :close() was never explicitly called on database 'XXXXXX'
来源:互联网 发布:windows ping 编辑:程序博客网 时间:2024/06/06 05:16
今天在做android项目的时候遇到了一个异常:
记录一下解决方案,方便今后自我查询,同时若还有其他解决方案,请伙伴们提出宝贵意见,一起学习。。。。。。
解决方案一:DBhelper类继承SQLiteOpenHelper类。做一个静态引用,确保在任何时候只存在一个DBhelper实例,代码如下:
public class DBhelper extends SQLiteOpenHelper {//数据库名private static final String DATABASE_NAME_STRING = "xxxxxx.db";//数据库版本private static final int DATABASE_VERSION = 1;private static DBhelper mDBhelper;public static DBhelper getInstance(Context context){if(null == mDBhelper){ mDBhelper = new DBhelper(context.getApplicationContext()); } return mDBhelper; }public DBhelper(Context context){ super(context,DATABASE_NAME_STRING,null,DATABASE_VERSION); } 。。。。。//数据库的其他操作 }
在创建数据库的activity或者自定义封装类中通过调用getInstance()来得到DBhelper实例。
//在需要创建的java类中声明private DBhelper helper;private SQLiteDatabase db;//在需要创建的方法中调用if(null == db){ helper = DBhelper.getInstance(context); db = helper.getWritableDatabase(); }
解决方案二:
1、在DBhelper即Database Adapter中添加close函数
public void close() { if (mDbHelper != null) { mDbHelper.close(); } }
2、复写activity的onDestroy函数,在onDestroy中关闭数据库即可
@Override protected void onDestroy() { super.onDestroy(); if (helper != null) { helper .close(); } }
希望此两个方案也能给遇到相同问题的伙伴提供一些思路,仅供参考。
0 0
- android SQLite :close() was never explicitly called on database 'XXXXXX'
- Android SQLite - close() was never explicitly called on database
- close() was never explicitly called on databaseandroid.database.sqlite异常
- FAQ_21 sqlite exception: close() was never explicitly called on database
- SQLite: close() was never explicitly called on database
- 解决android sqlite 中"close() was never explicitly called on database" 与java线程同步
- 处理Android SQLite - close() was never explicitly called on database异常
- 处理AndroidSQLite-close()was never explicitly called on database异常
- close() was never explicitly called on database,SQLiteMisuseException
- close() was never explicitly called on database 和 database is locked 错误原因
- close() was never explicitly called on database 和 database is locked 错误原因
- close() was never explicitly called on database 和 database is locked 错误原因
- close() was never explicitly called on database '/data/data/com.example.shiyan/databases/njupt.db'
- close() was never explicitly called on database '/data/data/com.seehope.view/databases/note.db' 异常
- Android异常解决--A WebView method was called on thread 'JavaBridge'. All WebView methods must be called
- android SQLite - close() was nev…
- android开发 WebViewjava.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebVie
- Android 在使用webview报错 A WebView method was called on thread 'Thread-2599'
- VC中定时器的使用
- PHP "完美"的防XSS 防SQL注入的代码
- TLD(Tracking-Learning-Detection)学习与源码理解之(六)
- 最好的8个 Java RESTful 框架
- ceph存储 Linux 编译安装Boost
- android SQLite :close() was never explicitly called on database 'XXXXXX'
- TLD(Tracking-Learning-Detection)学习与源码理解之(七)
- 常用css属性
- linux之间利用scp传输文件
- C++STL库的Vector容器简单实现的源代码参考
- Java多线程之----CountDownLatch
- 物理内存分配与回收(3) 之slab分配机制
- 理解MySQL——架构与概念
- Android——滚动效果(View内部滚动+拖动滚动)