Android--一个玩SimpleCursorAdapter的新手,我遇到了一些我无法解决的问题。
以下是代码(imports省略):
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
}
例如:这将打开一个列表视图,并在其中填充我所在地区的一堆县名称。单击一个县时,将启动另一个活动(具有几乎相同的代码),显示该县的市政当局。
大多数情况下,代码是有效的。每隔一段时间,LogView中就会弹出以下几行(每个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 \
或关闭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(本机方法)
我不明白为什么我得到了这些,但由于模拟器不会崩溃,我并没有太多的麻烦。
现在是关键错误:当我的列表视图启动时,我在一段时间内(可能是5分钟)没有单击任何单元格,然后单击,仿真器会严重崩溃,并在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.LocationDB.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
有人能告诉我这是怎么回事吗?
发布于 2011-10-11 18:59:10
如果不看代码就很难知道,但是您可能在关闭游标之前关闭了数据库:SQLite Exception in android
这就是错误所在,所以您应该在这里进行检查:在se.ribit.bb.LocationDB.updateTowns(LocationDB.java:291)
发布于 2011-10-11 19:04:58
释放OnDestroy方法中的光标
stopManagingCursor(c);
哦,你应该使用CursorLoader,startManagingCursor()已经被弃用了。
Loader API通过兼容性包提供给旧设备,请参阅以下内容:http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/
https://stackoverflow.com/questions/7724813
复制相似问题