在Android中使用SimpleCursorAdapter时出现异常和崩溃

时间:2011-10-11 10:48:06

标签: android debugging android-listview

Android-newbie玩SimpleCursorAdapter,我遇到了一些我无法解决的问题。

这是代码(省略导入):

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:这会打开一个列表视图,并在我的区域周围填充一堆县名。点击某个县时,会启动另一项活动(代码几乎完全相同),显示该县的市政当局。

代码主要起作用。偶尔会在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):在android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)     10-11 09:28:06.651:INFO / dalvikvm(244):at dalvik.system.NativeStart.run(Native Method)

我不明白为什么我会得到这些,但由于模拟器没有崩溃我没有太多麻烦。

现在出现严重错误:当我的listview启动并且我没有点击任何单元格一段时间(可能是5分钟),然后单击时,模拟器会崩溃并在LogCat中输出:< / p>

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

谁能告诉我这里发生了什么?

2 个答案:

答案 0 :(得分:1)

使用

释放OnDestroy方法中的Cursor
stopManagingCursor(c);

哦,你应该使用CursorLoader,不推荐使用startManagingCursor()。

Loader API带有兼容性套件的旧设备,请参阅:http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

答案 1 :(得分:0)

很难在不看代码的情况下知道,但您可能在关闭光标之前关闭了数据库:SQLite Exception in android

这是错误所在,所以你应该在那里检查:在se.ribit.bb.LocationDB.updateTowns(LocationDB.java:291)