我使用我的数据库助手:
@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();
}