gpt4 book ai didi

android - IllegalStateException:数据库未在第二个应用程序进入时打开,ORMLite

转载 作者:行者123 更新时间:2023-11-29 16:12:25 24 4
gpt4 key购买 nike

我使用我的数据库助手:

@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)
{
try
{

TableUtils.createTable(connectionSource, Category.class);
Log.i(DatabaseHandler.class.getName(), "created new entries in onCreate: " );
}
catch (SQLException e){
Log.e(TAG, "error creating DB " + DATABASE_NAME);
throw new RuntimeException(e);
}
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVer, int newVer)
{
try
{

TableUtils.dropTable(connectionSource, Category.class, true);
onCreate(db, connectionSource);
}
catch (SQLException e){
Log.e(TAG,"error upgrading db "+DATABASE_NAME+"from ver "+oldVer);
throw new RuntimeException(e);
}

}
public Dao<Category, Integer> getCategoryDao() throws SQLException
{
if (simpleCategoryDao == null) {
simpleCategoryDao = getDao(Category.class);
}
return simpleCategoryDao;
}

public static synchronized DatabaseHandler getHelper(Context context)
{
if (helper == null)
{
helper = new DatabaseHandler(context);
}
usageCounter.incrementAndGet();
return helper;
}

然后我获取 JSON 数据,将其包装到我的模型中并将其保存在 ORMLite 中。我在 AsyncTask 内部类中执行此操作。我的 doInBackground 方法在里面。

        protected Void doInBackground(String... param) {
ContentRepository _contactRepo;
UserFunctions userFunction = new UserFunctions();
json = userFunction.loginUser(param[0], param[1]);
try {
if ((json.getString(KEY_STATUS) != null) & json.getString(KEY_MESSAGE).equals((String)"Success"))
{

String res = json.getString(KEY_STATUS);
if(Integer.parseInt(res) == 1)
{

DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONParser p = new JSONParser();
appendLog(json.toString());
DataParsing u = new DataParsing();
List<Category> catlist = u.wrapCategories(json);
List<ProcessStatus> status = u.wrapStatuses(json);
List<Level> level = u.wrapLevels(json);
List<Subject> subject = u.wrapSubjects(json);

_contactRepo=new ContentRepository(getContentResolver(),getApplicationContext());

OrmLiteSqliteOpenHelper dbHelper=getHelper1();
getHelper1().open();

Dao<Category, Integer> daoCat=dbHelper.getDao(Category.class);
QueryBuilder<Category,Integer> catquery = daoCat.queryBuilder();
try{
getHelper1().open();
if (catquery.query().isEmpty())
{
_contactRepo.saveCategories(catlist);
}
}
catch (IllegalStateException e)
{
getHelper1().open();
e.printStackTrace();

}

Log.i("category",catquery.query().toString());

}
}

和 getHelper1() 方法:

private DatabaseHandler getHelper1() {
if (databaseHandler == null) {
databaseHandler = DatabaseHandler.getHelper(this);
}
return (DatabaseHandler) databaseHandler;
}

当我启动应用程序时,一切正常。当我第二次尝试启动它时,我得到:

 08-21 15:44:48.939: E/AndroidRuntime(1893): FATAL EXCEPTION: AsyncTask #2
08-21 15:44:48.939: E/AndroidRuntime(1893): java.lang.RuntimeException: An error occured while executing doInBackground()
08-21 15:44:48.939: E/AndroidRuntime(1893): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.lang.Thread.run(Thread.java:1019)
08-21 15:44:48.939: E/AndroidRuntime(1893): Caused by: java.lang.IllegalStateException: database not open
08-21 15:44:48.939: E/AndroidRuntime(1893): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1343)
08-21 15:44:48.939: E/AndroidRuntime(1893): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:154)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:206)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:155)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:261)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:262)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:329)
08-21 15:44:48.939: E/AndroidRuntime(1893): at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
08-21 15:44:48.939: E/AndroidRuntime(1893): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-21 15:44:48.939: E/AndroidRuntime(1893): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-21 15:44:48.939: E/AndroidRuntime(1893): ... 4 more
08-21 15:44:54.589: I/destroy(1893): destroy
08-21 15:44:54.839: E/WindowManager(1893): Activity com.assignmentexpert.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40668ff0 that was originally added here
08-21 15:44:54.839: E/WindowManager(1893): android.view.WindowLeaked: Activity com.assignmentexpert.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40668ff0 that was originally added here
08-21 15:44:54.839: E/WindowManager(1893): at android.view.ViewRoot.<init>(ViewRoot.java:263)
08-21 15:44:54.839: E/WindowManager(1893): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-21 15:44:54.839: E/WindowManager(1893): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-21 15:44:54.839: E/WindowManager(1893): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-21 15:44:54.839: E/WindowManager(1893): at android.app.Dialog.show(Dialog.java:241)
08-21 15:44:54.839: E/WindowManager(1893): at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-21 15:44:54.839: E/WindowManager(1893): at android.app.ProgressDialog.show(ProgressDialog.java:90)
08-21 15:44:54.839: E/WindowManager(1893): at com.assignmentexpert.LoginActivity$LoginTask.onPreExecute(LoginActivity.java:239)
08-21 15:44:54.839: E/WindowManager(1893): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-21 15:44:54.839: E/WindowManager(1893): at com.assignmentexpert.LoginActivity$5.onClick(LoginActivity.java:195)
08-21 15:44:54.839: E/WindowManager(1893): at android.view.View.performClick(View.java:2485)
08-21 15:44:54.839: E/WindowManager(1893): at android.view.View$PerformClick.run(View.java:9080)
08-21 15:44:54.839: E/WindowManager(1893): at android.os.Handler.handleCallback(Handler.java:587)
08-21 15:44:54.839: E/WindowManager(1893): at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 15:44:54.839: E/WindowManager(1893): at android.os.Looper.loop(Looper.java:123)
08-21 15:44:54.839: E/WindowManager(1893): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-21 15:44:54.839: E/WindowManager(1893): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 15:44:54.839: E/WindowManager(1893): at java.lang.reflect.Method.invoke(Method.java:507)
08-21 15:44:54.839: E/WindowManager(1893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-21 15:44:54.839: E/WindowManager(1893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-21 15:44:54.839: E/WindowManager(1893): at dalvik.system.NativeStart.main(Native Method)

但是,当我再次启动应用程序时 - 一切又恢复正常了。下一次 - 我再次遇到异常。

我在辅助类中的 open() 方法:

  public void open() throws SQLException {
helper.getReadableDatabase();
helper.getWritableDatabase();

}

最佳答案

所以所有的ORMLIte Android 示例具有类似的模式,我相信您可能会违反。您需要确保在应用程序关闭时删除任何缓存的 DAO。例如,HelloAndroid example在其 DatabaseHelper 中有以下方法。

public void close() {
super.close();
simpleDao = null;
simpleRuntimeDao = null;
}

我还看到您正在调用 getHelper1().open(); 但我不知道该方法在做什么。助手将在 Android 的 SQLiteOpenHelper 时打开——您不需要自己打开它。

我看到 getHelper() 返回一个 DatabaseHandler。我不知道那个类(class)。那是错字吗? OrmLiteBaseActivity 和类似类中内置了一个 getHelper() 方法,该方法知道何时使其无效并创建一个新方法。您应该确保您正在使用它们。

另外,我在辅助类本身上看到了一个 getHelper() 方法以及一个 usageCounter。我会回到 HelloAndroid example 的模式并删除所有这些定制,除非您有充分的理由拥有它们。您的自定义是这里令人困惑的地方。

关于android - IllegalStateException:数据库未在第二个应用程序进入时打开,ORMLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12055506/

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