SQLite: library routine called out of order - how to solve this problem?

Ok, so my application works fine on Android 2.3 and crashes on 4.0.4.

Im parsing the XML and storing the data in my SQLite database.

This does not happen every time , but in fact often , sometimes when you click a button, new launches of Activity sometimes appear ... etc.

Failure Exception:

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) 

ED I T: So, I closed all the cursors before closing all the db connections in my application, and now I have this:

  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) 

The problem part of my code is a function in my Top20 work, which is currently the following function:

 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 is my database for sure, I was looking for this error and of course this happens when I want to edit a value in a stream and repeat the same value with another stream (this time the AsyncTask background), but I have a singleton Database Adapter and I read here on the stack that this kind of database implementation is best for Android apps.

I have an extended application class with one database, and I call this database every time.

 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; } 

}

Link to the database in my applications:

 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:

My questions:

Why did this error occur? Am I using this single-user database implementation correctly?

Thank you for your advice, Adam.

+4
source share
1 answer

You seem to get a ConcurrentModificationException because you are trying to close the database when some other thread is trying to access it. Without seeing the rest of your code, I'm not sure if the other threads that you are accessing the database are so hard to tell what else is going on.

As an aside, and perhaps a better solution, is there a reason you are trying to close it? According to Diana Hackborn, a Google FW engineer, you don’t need to close the database, as it will be automatically closed after the process is complete. See link # 1 and link # 2 for several posts where she comments on this.

+4
source

Source: https://habr.com/ru/post/1446469/


All Articles