即使我尝试在堆栈上发布了一些解决方案,我也遇到了同样的错误。
这是我的logcat:
07-07 11:43:45.631: E/SQLiteDatabase(2841): close() was never explicitly called on database '/data/data/com.up.ridechaser/databases/DBs'
07-07 11:43:45.631: E/SQLiteDatabase(2841): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at com.up.ridechaser.DataBaseHelper.openDataBase(DataBaseHelper.java:67)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at com.up.ridechaser.MainActivity.onCreate(MainActivity.java:39)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.Activity.performCreate(Activity.java:4466)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.os.Looper.loop(Looper.java:137)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 11:43:45.631: E/SQLiteDatabase(2841): at dalvik.system.NativeStart.main(Native Method)
我尝试使用此代码在DATABASEHELPER中关闭我的光标和数据库:
编辑:
public class DataBaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
private static String DB_NAME = "DBs";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 10);
this.myContext = context;
this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
Log.e("Path 1", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
Log.e("YDB", "existOne");
} else {
this.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
File databasePath = myContext.getDatabasePath(DB_NAME);
return databasePath.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[10];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<String> getAll(String table, String column, int select, String[] selection, String[] value) {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = null;
if (select == 1) {
res = db.rawQuery("select * from " + table, null);
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex(column)));
res.moveToNext();
}
}
else {
res = db.rawQuery("SELECT * FROM " + table + " WHERE (" + selection[0] + "='" + value[0] + "')" + "AND ("
+ selection[1] + "='" + value[1] + "')", null);
res.moveToFirst();
try {
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex(column)));
res.moveToNext();
}
} catch (SQLiteException e) {
} finally {
res.close();
}
}
return array_list;
}
}
同时,我将此代码添加到我的所有活动中:
@Override
protected void onDestroy() {
super.onDestroy();
super.onDestroy();
if (myDbHelper != null) {
myDbHelper.close();
}
}
public void onPause() {
super.onPause();
if (myDbHelper != null) {
myDbHelper.close();
}
}
}
当我启动我的应用程序时,我没有错误,但是当我转到第二个活动时,会显示错误!
答案 0 :(得分:1)
崩溃日志抱怨你有一些打开游标或db最好是使用finally子句来关闭游标。只在某些生命周期方法中关闭db,例如onStop或onDestroy,如果你在已经关闭的db上调用close,则不能确定sqlite会做什么。
答案 1 :(得分:0)
android不要db close
android系统默认auto db close
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex(column)));
res.moveToNext();
}
-->
while(res.moveToNext()){
array_list.add(res.getString(res.getColumnIndex(column)));
}
you can
try{
while(res.moveToNext()){
array_list.add(res.getString(res.getColumnIndex(column)));
}
}
catch(SqlException e){
}
finlly{
res.close();
}