1

我使用我的数据库助手:

@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();

  }
4

3 回答 3

3

所以所有的ORMLIte Android 例子都有一个相似的模式,我相信你可能会违反。您需要确保在应用程序关闭时删除所有缓存的 DAO。例如,HelloAndroid 示例在其DatabaseHelper.

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

我也看到你在打电话getHelper1().open();,但我不知道那个方法在做什么。助手将在 Android 的时候打开SQLiteOpenHelper——你不需要自己打开它。

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

此外,我getHelper()在帮助类本身上看到了一个方法以及一个usageCounter. 我会回到HelloAndroid 示例中的模式并删除所有这些自定义项,除非您有充分的理由拥有它们。您的自定义是这里令人困惑的事情。

于 2012-08-21T22:19:14.807 回答
1

尝试:

 DatabaseHelper db = getHelper1();

在您getHelper1()返回一个DatabaseHelper类类型,但您已将其分配给 OrmLiteSqliteOpenHelper。您似乎也没有使用该db变量。

希望这可以帮助。

于 2012-08-21T13:14:22.360 回答
0

通常,当您在关闭 ormlite 数据库时尝试从数据库中读取或写入某些内容时,您会遇到此问题。即使我遇到了这个错误。

确保当您尝试读取或写入操作时,您必须拥有dbhelper.getreadable()getwritable()为此。

于 2012-08-21T13:20:25.467 回答