SQLite Exception on Resuming Android Activity

2019-02-23 09:13发布

问题:

My app has a hierarchy of Activities, A -launches- B -launches- C

In the third Activity 'C' I have a button.

In the onClickListener of this button I launch an Intent as follows:

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(uri));
startActivity(intent);
//I don't call finish()

It brings up the phone dialling dialog fine. If I hit the back button at this point I get the "Sorry!" popup saying my app stopped unexpectedly. On hitting the "Force Close" button my app reverts to the Activity 'B' rather then the expected 'C' (Assuming no crash).

Activity 'B' does use the SQL query shown in the exception however I don't know why it is causing the exception when I hit 'Back' as it has nothing to do with Activity 'C'. My database has been closed and I don't get an Leak warnings.

In Activity 'B' the database is opened immediately prior to executing the query and closed afterward. Struggling with this all day so would appreciate any comments.

 Uncaught handler: thread main exiting due to uncaught exception
 java.lang.IllegalStateException: mQuery SELECT islocal, packageid, Name, mapradius FROM categories WHERE islocal=? 1 
     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:162)
     at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:536)
     at android.app.Activity.performRestart(Activity.java:3740)
     at android.app.ActivityThread.handleWindowVisibility(ActivityThread.java:3312)
     at android.app.ActivityThread.access$2600(ActivityThread.java:123)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1890)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4370)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence: handle 0x0
     at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:178)
     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:153)
     ... 13 more
ERROR/SemcCheckin(17282): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump

回答1:

I just solved this exception in my app. Not sure if the cause was the same as yours...

Activity A was performing a query JOIN on 2 tables in the database via a bridge table. The records from that join were put into a ListView and when a user clicks a record, my app fires off Activity B. When you hit the back button on Activity B, resuming Activity A this exception happened.

The fix was to explicitly close the cursor (cur.close()) in Activity A when it was finished retrieving data. Before I was just closing the DB.

This DID NOT happen if Activity A did a basic query without a JOIN. So it seems when you join multiple tables and return in a cursor, the requery that the system performs on that open cursor when your task resumes generates this exception. You have to explicitly close the cursor so that the resume establishes a new one. Another factor that could have caused this is that my DB access in Activity A happens on a separate worker thread.



回答2:

The problem turned out to be a concurrency issue relating to open connections to the database. Still not entirely sure about the root cause as I always closed my connections. Anyway, I created a single connection that is shared across all activities and the problem has gone away.