Exceptions and crashes when using SimpleCursorAdapter in Android
Android-newbie playing around with a SimpleCursorAdapter and I've run into a few problems that I'm unable to solve.
This is the code (imports omitted):
package se.ribit.bb;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class CountyBrowser extends Activity {
private AVApplication audiovideo;
private final String LOG_TAG = AVApplication.LOG_TAG;
private final String TAG = "=== " + CountyBrowser.class.getSimpleName() + ": ";
static final String[] FROM = { LocationDB.C_NAME};
static final int[] TO = { R.id.locationName };
// DB dbHelper;
private Cursor cursor;
private ListView listItems;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, TAG + "onCreate");
setContentView(R.layout.county_main_view);
// View for the adapter
listItems = (ListView) findViewById(R.id.county_listview);
// Get the application context
audiovideo = (AVApplication) getApplication();
audiovideo.httpCommand(Commands.GET_COUNTIES, null);
}
@Override
protected void onResume() {
super.onResume();
showCounties();
}
public void showCounties() {
Log.d(LOG_TAG, TAG + "refreshItemsList();");
// Get a cursor with all item updates
cursor = audiovideo.getLocationDB().getCounties();
startManagingCursor(cursor);
// Setup the adapter
adapter = new SimpleCursorAdapter(this, R.layout.county_listview_item, cursor, FROM, TO);
listItems.setAdapter(adapter);
listItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int countyID = cursor.getInt(cursor.getColumnIndex(LocationDB.C_ID));
String countyName = cursor.getString(cursor.getColumnIndex(LocationDB.C_NAME));
String foo = String.format(TAG + "Clicked ID #%d (%s)", countyID, countyName);
Log.i(LOG_TAG, foo);
// Show the item in a new activity
Intent apan = new Intent(audiovideo, MunicipalBrowser.class);
apan.putExtra("countyID", countyID);
startActivity(apan);
}
});
} // refreshItemsList
}
I.e: This opens a listview and fills it with a bunch of county-names around my area. When clicking a county, another activity (with almost identical code) is launched, showing the municipals in the county.
The code works, mostly. Every now and then these lines pops up in LogView (one set for each listview-cell):
10-11 09:28:06.601: INFO/dalvikvm(244): Uncaught exception thrown by finalizer (will be discarded):
10-11 09:28:06.641: INFO/dalvikvm(244): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c7e808 on null that has not been deactivated \
or closed 10-11 09:28:06.651: INFO/dalvikvm(244): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596) 10-11 09:28:06.651: INFO/dalvikvm(244): at dalvik.system.NativeStart.run(Native Method)
I don't understand why I get these, but since the emulator doesn't crash I didn't bother too much.
Now for the critical error: When my listview is launched and I don't click any of the cells for some amount of time (maybe 5 minutes), and then click, the emulator crashes hard and outputs this in LogCat:
10-11 11:45:22.591: ERROR/AndroidRuntime(255): Uncaught handler: thread main exiting due to uncaught exception
10-11 11:45:22.682: ERROR/AndroidRuntime(255): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.ribit.bb/se.ribit.bb.TownBrowser}: android.database.sqlite.SQLiteException: Unable to close due to unfinalised statements
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.os.Looper.loop(Looper.java:123)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at java.lang.reflect.Method.invoke(Method.java:521)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at dalvik.system.NativeStart.main(Native Method)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): Caused by: android.database.sqlite.SQLiteException: Unable to close due to unfinalised statements
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:254)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:42)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.database.sqlite.SQLiteProgram.onAllReferencesReleasedFromContainer(SQLiteProgram.java:76)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:799)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:786)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at se.ribit.bb.Locatio开发者_C百科nDB.updateTowns(LocationDB.java:291)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at se.ribit.bb.AVApplication.httpCommand(AVApplication.java:217)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at se.ribit.bb.TownBrowser.onCreate(TownBrowser.java:44)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-11 11:45:22.682: ERROR/AndroidRuntime(255): ... 11 more
Can anyone tell me what's going on here?
Do your release the Cursor in your OnDestroy method with
stopManagingCursor(c);
?
oh and your should use the CursorLoader, startManagingCursor() is deprecated.
The Loader API comes to old devices with the compatiblity Package, See this: http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/
It's hard to know without seeing code, but you are probably closing the database before closing the cursor: SQLite Exception in android
This is where the error is, so you should inspect there: at se.ribit.bb.LocationDB.updateTowns(LocationDB.java:291)
精彩评论