游标在ListFragment中没有事先close()而最终确定

时间:2013-07-07 14:27:28

标签: android sqlite android-listfragment

我在关闭应用程序后收到此错误消息,或者只是让手机在应用程序打开时休眠。

当我让手机睡眠时,我会收到此错误消息的30到61个日志条目。在加载ListFragment之前记录错误。我怀疑片段被加载但是在片段的信息条目之前记录了错误。

当我关闭应用程序时,我只得到一个错误的输入,该错误是在onLoaderReset()调用之后记录的。我在关闭应用程序之前关闭了数据库,这似乎有所帮助。

该应用程序有效,但我担心一旦在各种手机上运行它将失败。

除了onLoaderReset()中的adapter.swapCursor(null)之外,我没有显式控制游标。

我很感激有关如何解决此错误的任何想法。

由于

public class ChildList extends ListFragment implements
    LoaderManager.LoaderCallbacks<Cursor>, DatabaseConstants, GoodiList {

private final String TAG = "ChildList";

// database columns that we will retreive
final String[] PROJECTION = new String[] { CHILD_ID_COLUMN,
        CHILD_NAME_COLUMN };

// selects all
final String SELECTION = null;

SimpleCursorAdapter adapter = null;

private ListParent parentActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.e(TAG, "on Create");
    String[] fromColumns = { CHILD_NAME_COLUMN, CHILD_ID_COLUMN };

    int[] toViews = { R.id.name_column, R.id.id_column };

    adapter=new SimpleCursorAdapter(
            getActivity(),
            R.layout.child_list_entry,
            null,
            fromColumns,
            toViews,
            0
            );

    setListAdapter(adapter);

    getLoaderManager().initLoader(0, null, this);

    Log.i(TAG, "finished on Create");
}


public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    LongClickListener longClickListener = new LongClickListener();

    getListView().setOnItemLongClickListener(longClickListener);
}

@Override
public void setListAdapter(ListAdapter adapter) {
    super.setListAdapter(adapter);
}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

    Log.v(TAG, "starting on create loader");

    Uri CHILD_URI = ChildContentProvider.CONTENT_URI;

    CursorLoader cursorLoader = new CursorLoader(getActivity(), CHILD_URI,
            PROJECTION, SELECTION, null, null);

    Log.v(TAG, "finished on create loader");

    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor newCursor) {
    adapter.swapCursor(newCursor);

    if (newCursor != null) {
        int rowCount = newCursor.getCount();

        Log.v(TAG, "--- onLoadFinished:  got " + rowCount + " children");
    }
}

/**
 * this used to delete. However, we need it to select usually. Now delete is
 * done by long click.
 */
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // delete child - remove child from list and database
    Object item = l.getItemAtPosition(position);

    Cursor cursor = (Cursor) item;

    int nameCol = cursor.getColumnIndex(CHILD_NAME_COLUMN);
    int idCol = cursor.getColumnIndex(CHILD_ID_COLUMN);

    final String name = cursor.getString(nameCol);

    final int childId = cursor.getInt(idCol);

    Log.i(TAG, "selected " + name + " id: " + childId + " at position "
            + position);

    cursor.close();

    parentActivity.listItemSelected(this, position, childId);

}

/**
 * Forces cursor to requery database and list to be update. Used when a new
 * child is entered in parent activity's EditText field.
 */
public void notifyDataChanged() {

    Log.i(TAG, "told adapter that data changed");

    getLoaderManager().restartLoader(0, null, this);

    adapter.notifyDataSetChanged();
}

/** tell adapter that cursor is no longer valid*/
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
Log.i(TAG,"---- onLoaderReset -----");

adapter.swapCursor(null);

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    parentActivity = (ListParent) activity;

    parentActivity.setList(this);

}

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我仔细查看了文档解决了这个问题。

根据文档http://developer.android.com/reference/android/widget/CursorAdapter.html#swapCursor(android.database.Cursor)

方法swapCursor(Cursor data)返回原始的未闭合游标。如果新游标与旧游标相同或者没有旧游标,则返回null。

通过更改我的代码

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    Cursor cursor = adapter.swapCursor(data);
    if (cursor != null) {
        cursor.close();
    }
}

我停止了这些警告的出现。

注意:您也应该对onLoaderReset()方法执行相同的操作。即使您执行adapter.swapCursor(null),它也会返回旧光标。

答案 1 :(得分:0)

您应该使用changeCursor(Cursor cursor)代替swapCursor(Cursor cursor)

如果查看documentation,您会看到它也会关闭旧光标。