4

好的,所以我的应用程序在 Android 2.3 上完美运行,在 4.0.4 上崩溃。

我正在解析 XML 并将数据保存到我的 SQLite 数据库。

这不是每次都会发生,但确实经常发生,有时在按钮点击时有时在新的 Activity 启动时......等等

崩溃异常:

11-16 15:16:41.231: E/SQLiteQuery(6398): exception: library routine called out of sequence; query: SELECT _id, name, tipus, azonosito, gpsx, gpsy, pictures, telefonok, webcim, email, nyitvatartas, cim, orszag, befogadokepesseg, szolgaltatasok, arkategoria, leiras, ertekeles, pontszam, facebook, keywords, rating FROM shops_top20

11-16 15:16:41.241: E/AndroidRuntime(6398): FATAL EXCEPTION: main
11-16 15:16:41.241: E/AndroidRuntime(6398): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KeySoft.OpenGuide/com.KeySoft.OpenGuide.Activities.Top20}: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.os.Looper.loop(Looper.java:137)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.ActivityThread.main(ActivityThread.java:4507)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at java.lang.reflect.Method.invoke(Method.java:511)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at dalvik.system.NativeStart.main(Native Method)
11-16 15:16:41.241: E/AndroidRuntime(6398): Caused by: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.database.sqlite.SQLiteQuery.nativeFillWindow(Native Method)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:86)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:201)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at com.KeySoft.OpenGuide.Activities.Top20.SqlShopsVissza(Top20.java:546)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at com.KeySoft.OpenGuide.Activities.Top20.listakKirajzolasaEsValtas(Top20.java:165)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at com.KeySoft.OpenGuide.Activities.Top20.onCreate(Top20.java:113)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.Activity.performCreate(Activity.java:4469)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
11-16 15:16:41.241: E/AndroidRuntime(6398):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)

编辑:好的,所以我在关闭应用程序中的所有数据库连接之前关闭了所有游标,现在我得到了这个例外:

    11-16 20:00:28.409: E/AndroidRuntime(7548): FATAL EXCEPTION: main
11-16 20:00:28.409: E/AndroidRuntime(7548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KeySoft.OpenGuide/com.KeySoft.OpenGuide.Activities.Top20}: java.util.ConcurrentModificationException
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.os.Looper.loop(Looper.java:137)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.ActivityThread.main(ActivityThread.java:4507)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at java.lang.reflect.Method.invoke(Method.java:511)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at dalvik.system.NativeStart.main(Native Method)
11-16 20:00:28.409: E/AndroidRuntime(7548): Caused by: java.util.ConcurrentModificationException
11-16 20:00:28.409: E/AndroidRuntime(7548):     at java.util.WeakHashMap$HashIterator.next(WeakHashMap.java:165)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1250)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:1220)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.database.sqlite.SQLiteOpenHelper.close(SQLiteOpenHelper.java:265)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at com.KeySoft.OpenGuide.DB_Adapter.close(DB_Adapter.java:273)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at com.KeySoft.OpenGuide.Activities.Top20.SqlShopsVissza(Top20.java:558)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at com.KeySoft.OpenGuide.Activities.Top20.listakKirajzolasaEsValtas(Top20.java:165)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at com.KeySoft.OpenGuide.Activities.Top20.onCreate(Top20.java:113)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.Activity.performCreate(Activity.java:4469)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
11-16 20:00:28.409: E/AndroidRuntime(7548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)

我的代码中有问题的部分是我的 Top20 Activity 中的一个函数,目前是以下函数:

protected void getShopsFromSql(String whichTable){


            db.open();
            Cursor c;

            if(whichTable.equals("Top20")){
                c = db.getAllTop20Titles();
            }
            else if (whichTable.equals("Latest10")){
                c = db.getAllLatest10Titles();
            }
            else{
                c = db.getAllShopsFullTitles();
            }

                if (c.moveToFirst())  {
                    do  {

                        addShopToList(c.getString(1), c.getString(2), c.getString(4), c.getString(5), c.getString(12), c.getString(11), c.getString(3));
                    }while (c.moveToNext());
                }

                c.close(); 
                db.close(); //App Crashes here... why?
        }

db 肯定是我的数据库,我用谷歌搜索了这个错误,当然,当我想在一个线程上编辑一个值并通过另一个线程(这次是后台 AsyncTask)迭代相同的值时会发生这种情况,但我有一个单例数据库适配器我在堆栈中读到这种数据库实现最适合 Android 应用程序。

我有一个带有单个数据库的扩展应用程序类,我每次都调用这个数据库。

public class MyApp extends Application {

 private static DB_Adapter globalDatabase;


    @Override
    public void onCreate() {
        super.onCreate();

        globalDatabase = new DB_Adapter(this);
    }

    public static DB_Adapter getDB() {

        return globalDatabase;
    }

}

在我的应用程序中引用数据库,如下所示:

public class Top20 extends Activity 
{


    private DB_Adapter db;
    private MyApp myApp;

    @Override
    public void onCreate(Bundle savedInstanceState){



        super.onCreate(savedInstanceState);
        setContentView(R.layout.top20);


        myApp = (MyApp) getApplication();

        db = myApp.getDB();

                    //...........morecode...etc..

所以:

我的问题是:

为什么会出现这个错误?我做对了这个单例数据库实现吗?

谢谢你的建议,亚当

4

1 回答 1

4

你似乎得到了,ConcurrentModificationException因为你试图关闭数据库,因为其他线程试图访问它。没有看到你的代码的其余部分,我不确定你有哪些其他线程访问数据库,所以很难知道还有什么事情发生。

顺便说一句,可能是更好的解决方案,您是否有理由尝试关闭它?正如 Google FW 工程师 Dianne Hackborn 所说,您不需要关闭数据库,因为一旦您的进程被终止,它将自动关闭。有关她对此发表评论的多个帖子,请参阅链接 #1链接 #2

于 2014-01-07T23:04:11.130 回答