gpt4 book ai didi

java.lang.IllegalStateException : attempt to re-open an already-closed?

转载 作者:行者123 更新时间:2023-12-01 18:12:28 26 4
gpt4 key购买 nike

我的 Sqlite Android 出现错误。我有这样的代码:

public int UpdatePhotoUser(String foto_user, int metode) {

SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
int update = 0;

if (metode == 1) {
values.put(Variabel.KEY_USER_PHOTO, foto_user);
try {
update = db.update(
Variabel.TABLE_USER,
values, null, null);
} catch (android.database.sqlite.SQLiteException e) {

}
} else if (metode == 2 && getUserDetails(Variabel.KEY_USER_PHOTO).equals(foto_user)) {
values.put(Variabel.KEY_USER_PHOTO, "");
try {
update = db.update(
Variabel.TABLE_USER,
values, null, null);
} catch (android.database.sqlite.SQLiteException e) {

}
}

db.close();
return update;
}

我在最后一行有错误:

update = db.update(
Variabel.TABLE_USER,
values, null, null);

这是错误:

Java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.ad.kamardagang.newseditor/databases/DB_KAMAR_DAGANG
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1545)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1522)
at com.ad.kamardagang.newseditor.database_content.DatabaseHandler.UpdatePhotoUser(DatabaseHandler.java:613)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment.removeRespone(PhotoUserFragment.java:985)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment.access$600(PhotoUserFragment.java:62)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment$12.onResponse(PhotoUserFragment.java:879)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment$12.onResponse(PhotoUserFragment.java:875)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

已编辑:我打电话的例子:

db = new DatabaseHandler(getActivity());
db.UpdatePhotoUser("http://a.png",1);

什么时候。我首先调用metode UpdatePhotoUser(String foto_user, int metode),但在第二调用它的错误。那么如何解决呢?

最佳答案

问题是您在方法结束时关闭数据库:

db.close();

你不需要这样做。当不再使用应用程序时,数据库将关闭。

无论如何,有更好的方法来管理数据库。搜索有关 ContentProvider 类的教程,那里有一些有用的资源。

关于java.lang.IllegalStateException : attempt to re-open an already-closed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com