1

请帮忙。

我正在学习使用 SimpleCursorAdaptor 来填充 ListView。我有一个带有 ListView 的 Activity,我使用 SimpleCursorAdapter 填充它。当我单击其中一个列表项时,它应该启动另一个 Activity,但是一旦新 Activity 启动,我就会得到以下日志:

01-11 15:28:31.847: E/AndroidRuntime(996): FATAL EXCEPTION: main
01-11 15:28:31.847: E/AndroidRuntime(996): android.database.StaleDataException: Attempted to access a cursor after it has been closed.
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.database.BulkCursorToCursorAdaptor.getCount(BulkCursorToCursorAdaptor.java:70)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:196)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:162)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.support.v4.widget.CursorAdapter.getItemId(CursorAdapter.java:225)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.widget.AbsListView.onSaveInstanceState(AbsListView.java:1782)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.View.dispatchSaveInstanceState(View.java:11839)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2576)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.widget.AdapterView.dispatchSaveInstanceState(AdapterView.java:782)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.view.View.saveHierarchyState(View.java:11822)
01-11 15:28:31.847: E/AndroidRuntime(996):  at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1566)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.Activity.onSaveInstanceState(Activity.java:1188)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:498)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.Activity.performSaveInstanceState(Activity.java:1137)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2951)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3010)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.ActivityThread.access$900(ActivityThread.java:130)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1222)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.os.Looper.loop(Looper.java:137)
01-11 15:28:31.847: E/AndroidRuntime(996):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-11 15:28:31.847: E/AndroidRuntime(996):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 15:28:31.847: E/AndroidRuntime(996):  at java.lang.reflect.Method.invoke(Method.java:511)
01-11 15:28:31.847: E/AndroidRuntime(996):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-11 15:28:31.847: E/AndroidRuntime(996):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-11 15:28:31.847: E/AndroidRuntime(996):  at dalvik.system.NativeStart.main(Native Method)

我看了看,但无法弄清楚这个错误是从哪里来的。

我在谷歌上搜索了这个 BulkCursorToCursorAdaptor 类,我发现它在不同的情况下出于不同的原因抛出了这个异常。

我想知道您是否可以解释错误的来源,因为我没有关闭适配器中的光标。

以下是代码片段:

ListView 的活动:

MyAdapter adapter;
ListView list;

@Override
public void onCreate(Bundle savedInstanceState)
{
    ....
    list = (ListView)findViewById(R.id.myList);
    list.setOnItemClickListener(new OnItemClickListener()
    {
       @Override
        public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long myLong)
        {
              Intent request = new Intent(MyActivity.this, NewActivity.class);
              startActivity(request);
        }
    }

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

    adapter = new MyAdapter(this,
                R.layout.list_item,
                null, new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME},
                new int[] {R.id.Name}, Adapter.NO_SELECTION);
    list.setAdapter(adapter);
}


@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle)
{
    return new CursorLoader(this,
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME},
                null, null, null);
}

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

@Override
public void onLoaderReset(Loader<Cursor> aCursor)
{
    adapter.swapCursor(null);
}

这是适配器:

public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)
{
    super(context, layout, c, from, to, flags);
}

@Override
public int getCount()
{
    Cursor c = getCursor(); // without this, sometimes, I get NullPointerException
    return c.getCount();
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    Cursor c = getCursor();
    c.moveToPosition(position);
    ....
    ....
}

请帮忙

4

0 回答 0