Android 导入数据库后,从未对数据库显式调用close()
我的应用程序中出现以下错误。但是,这个错误不会使我的应用程序崩溃。尽管如此,我知道是什么导致了这个错误,但不知道如何修复它。我之所以更加注意这个错误,是因为我正在将数据库从Asset文件夹处理到/data/文件夹。我在整个应用程序中使用相同的代码,以确保数据库存在,如果不再次复制它的话。如果用户错误地删除了数据库。这就是我使用该代码的原因(我将在错误下方跳过它)。我做了一项研究,这可能是我的应用程序因内存不足错误而崩溃的原因之一。不确定这是否也会导致内存泄漏。请帮帮我。正如我之前所说,在出现outofmemory错误之前,该应用程序工作正常。我想修复从这个开始发现的所有错误 错误:Android 导入数据库后,从未对数据库显式调用close(),android,database,runtime-error,assets,Android,Database,Runtime Error,Assets,我的应用程序中出现以下错误。但是,这个错误不会使我的应用程序崩溃。尽管如此,我知道是什么导致了这个错误,但不知道如何修复它。我之所以更加注意这个错误,是因为我正在将数据库从Asset文件夹处理到/data/文件夹。我在整个应用程序中使用相同的代码,以确保数据库存在,如果不再次复制它的话。如果用户错误地删除了数据库。这就是我使用该代码的原因(我将在错误下方跳过它)。我做了一项研究,这可能是我的应用程序因内存不足错误而崩溃的原因之一。不确定这是否也会导致内存泄漏。请帮帮我。正如我之前所说,在出现ou
01-08 17:22:44.326: E/Database(414): close() was never explicitly called on database '/data/data/com.drakeillusion.yao1/databases/YAOMasterDB.db'
01-08 17:22:44.326: E/Database(414): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-08 17:22:44.326: E/Database(414): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
01-08 17:22:44.326: E/Database(414): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
01-08 17:22:44.326: E/Database(414): at com.example.yao.YAOMySQLiteHelper.openDataBase(YAOMySQLiteHelper.java:328)
01-08 17:22:44.326: E/Database(414): at com.example.yao.SplashActivity.copydbfromassest(SplashActivity.java:128)
01-08 17:22:44.326: E/Database(414): at com.example.yao.SplashActivity.onCreate(SplashActivity.java:68)
01-08 17:22:44.326: E/Database(414): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-08 17:22:44.326: E/Database(414): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-08 17:22:44.326: E/Database(414): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-08 17:22:44.326: E/Database(414): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-08 17:22:44.326: E/Database(414): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-08 17:22:44.326: E/Database(414): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 17:22:44.326: E/Database(414): at android.os.Looper.loop(Looper.java:123)
01-08 17:22:44.326: E/Database(414): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-08 17:22:44.326: E/Database(414): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 17:22:44.326: E/Database(414): at java.lang.reflect.Method.invoke(Method.java:521)
01-08 17:22:44.326: E/Database(414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-08 17:22:44.326: E/Database(414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-08 17:22:44.326: E/Database(414): at dalvik.system.NativeStart.main(Native Method)
这是我的“DatabaseHelper”,名为YAOMySQLiteHelper。
YAOMySQLiteHelper包含大量代码,我将在相应的章节中介绍
public class YAOMySQLiteHelper extends SQLiteOpenHelper {
public static String db_path;
private String dbName;
private String dbNameFK;
private String dbImages;
//private Context context;
private final Context context;
private SQLiteDatabase myDataBase;
private static final String DATABASE_NAMEFK = "YAOMasterDB.jet";
private static final String DATABASE_NAME = "YAOMasterDB.db";
public YAOMySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.dbNameFK = DATABASE_NAMEFK;
this.dbName = DATABASE_NAME;
// this.dbImages=DATABASE_IMAGES;
this.context = context;
db_path = "/data/data/" + context.getPackageName() + "/databases/";
}
public void importIfNotExist() throws IOException {
boolean dbExist = checkExist();
if (dbExist) {
Log.i("Tag : ImportIfNotExist", "DB Exist : " + dbExist);
// do nothing - database already exist
} else {
Log.i("Tag : ImportIfNotExist", "DB Not Exist : " + dbExist);
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDatabase();
//Deleteing a file (Database)
// DeleteDBjet();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/*
* Create a copy of the database from Asset folder to the android emulator
*/
private void copyDatabase() throws IOException {
Log.i("Tag : copyDatabase - Ininitail database", "Getting ready to Coping Database From the database stored in Asset folder" );
//Open your local db as the input stream
InputStream is = context.getAssets().open(dbNameFK);
String RenameDB2OriginalName = "YAOMasterDB.db";
String outFileName = db_path + RenameDB2OriginalName;
//Open the empty db as the output stream
OutputStream os = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[10000];
Log.i("Tag : copyDatabase - buffer Database", "Coping Database From the database stored in Asset folder" );
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
this.close();
}
public boolean checkExist() {
SQLiteDatabase checkDB = null;
try {
String myPath = db_path + dbName;
File dbFile = new File( db_path + dbName);
return dbFile.exists();
}
catch (SQLiteException e) {
e.printStackTrace();
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = db_path + dbName;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
public void DeleteDBjet(){
String myPath = db_path + dbName;
File file = new File(myPath);
boolean deleted = file.delete();
}
在您的
SplashActivity
(警告来自哪里)myHelper.close()
永远不会被调用。如果您打开数据库,或者如果您正在全局处理它,则需要关闭数据库,这样您就不必一次又一次地打开它。但当不再在活动中使用时,关闭数据库将是一个更好的选择
改变方法
private void startApp() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
对此
private void startApp() {
myhelper.close();
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
谢谢,我回去照你说的做了,并把它放在相应的myhelper.close()中,那个错误就不再出现了。
private void startApp() {
myhelper.close();
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}