我正在开发将数据保存到SQLite的Android应用,并且我在其他应用上一直使用此方法,但是这引发了此异常:
但是添加新行的方法直到完成所有操作后才会关闭数据库。
我已经搜索了这个问题,大多数答案是“在此之后进行操作时您关闭了数据库”,情况并非如此。
此方法适用于我的其他Android应用程序,但在此方法中不起作用。
添加行,打开数据库,然后关闭数据库代码:
/**
* Adds a camera device.
*
* @param device The Device Class whcih will be saved in the database.
*/
public final void createEntry(final Device device) throws Exception {
open();
final ContentValues values = new ContentValues();
values.put(devicesColumns[1], AESCryptor.encrypt(device.getDeviceName()));
values.put(devicesColumns[2], AESCryptor.encrypt(device.getDeviceUrl()));
values.put(devicesColumns[12], AESCryptor.encrypt(device.getDdnsURL()));
values.put(devicesColumns[3], AESCryptor.encrypt(device.getUser().getUsername()));
values.put(devicesColumns[4], AESCryptor.encrypt(device.getUser().getPassword()));
values.put(devicesColumns[5], AESCryptor.encrypt(new Gson().toJson(device.getCamera())));
values.put(devicesColumns[6], AESCryptor.encrypt(new Gson().toJson(device.getWlan())));
values.put(devicesColumns[7], AESCryptor.encrypt(device.getLocalPort()));
values.put(devicesColumns[8], AESCryptor.encrypt(device.getDDNSPort()));
values.put(devicesColumns[9], AESCryptor.encrypt(device.getMotioneyeVersion()));
values.put(devicesColumns[10], AESCryptor.encrypt(device.getMotionVersion()));
values.put(devicesColumns[11], AESCryptor.encrypt(device.getOsVersion()));
String insertId = "ID = " + database.insert("DEVICES", null, values);
if (device.getID() != null) {
delete_item(device.getID());
insertId = device.getID();
}
final Cursor cursor = database.query("DEVICES", devicesColumns, insertId, null,
null, null, null);
cursor.close();
close();
}
public final void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public final void close() {
dbHelper.close();
}
日志:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.developerfromjokela.motioneyeclient/databases/Devices.db
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1234)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1110)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1278)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at com.developerfromjokela.motioneyeclient.database.Source.createEntry(Source.java:108)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at com.developerfromjokela.motioneyeclient.ui.setup.activities.SetupStartScreen.fillItems(SetupStartScreen.java:359)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at com.developerfromjokela.motioneyeclient.ui.setup.activities.SetupStartScreen.access$500(SetupStartScreen.java:89)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at com.developerfromjokela.motioneyeclient.ui.setup.activities.SetupStartScreen$3$1.run(SetupStartScreen.java:217)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.os.Looper.loop(Looper.java:164)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6494)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
2019-04-23 13:55:32.460 9621-9621/com.developerfromjokela.motioneyeclient W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)