我使用databaseHelper:
@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)
但是当我再次启动应用程序时 - 一切正常。在下一次 - 我再次获得例外。
helper类中的open()方法:
public void open() throws SQLException {
helper.getReadableDatabase();
helper.getWritableDatabase();
}
答案 0 :(得分:3)
因此,所有ORMLIte Android示例都有类似的模式,我相信您可能会违反这些模式。您需要确保在关闭应用程序时删除任何缓存的DAO。例如,HelloAndroid example在其DatabaseHelper
中有以下方法。
public void close() {
super.close();
simpleDao = null;
simpleRuntimeDao = null;
}
我也看到你正在调用getHelper1().open();
,但我不知道该方法在做什么。 Android的SQLiteOpenHelper
将打开帮助程序 - 您不需要自己打开它。
我看到getHelper()
返回DatabaseHandler
。我不知道那堂课。那是一个错字吗?内置于OrmLiteBaseActivity
和类似类中的是getHelper()
方法,该方法知道何时使其无效并创建新的方法。你应该确保使用它们。
另外,我在辅助类本身上看到getHelper()
方法以及usageCounter
。我会回到HelloAndroid example的模式并删除所有这些自定义,除非你有充分的理由让它们。您的自定义设置是令人困惑的事情。
答案 1 :(得分:1)
尝试:
DatabaseHelper db = getHelper1();
在getHelper1()
中,您返回DatabaseHelper
类类型,但已将其分配给OrmLiteSqliteOpenHelper。您似乎也没有使用db
变量。
希望这有帮助。
答案 2 :(得分:0)
当您在关闭ormlite数据库时尝试从数据库读取或写入内容时,通常会遇到此问题。即使我遇到了这个错误。
确保在尝试读取或写入操作时,您必须拥有dbhelper.getreadable()
或getwritable()
。