SQLite:库程序不按顺序调用 - 如何解决此崩溃?

时间:2012-11-16 14:21:14

标签: android sqlite android-sqlite

好的,所以我的应用程序完全适用于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)

E D I T: 好吧,所以我在关闭我的应用程序中的所有数据库连接之前关闭了所有游标,现在我得到了这个结果:

    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活动中的一个功能,它目前是以下功能:

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肯定是我的数据库,我用Google搜索了这个错误当然,当我想在一个线程上编辑一个值并通过另一个线程迭代相同的值(这次是背景AsyncTask)时,它会发生,但我有一个单例数据库适配器和我在堆栈中读到这种数据库实现最适合Android应用程序。

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

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..

SO:

我的问题是:

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

谢谢你的建议, 亚当

1 个答案:

答案 0 :(得分:4)

您似乎正在获取ConcurrentModificationException,因为您尝试关闭数据库,因为其他某个线程正在尝试访问它。在没有看到你的其余代码的情况下,我不确定你有什么其他线程访问数据库,所以很难分辨出其他的是什么。

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