Please help.
I am learning to use SimpleCursorAdaptor to populate a ListView. I have an Activity with a ListView that I populate using a SimpleCursorAdapter. When I click on one of the list items, it is supposed to start another Activity but as soon as the new Activity starts I get the following log:
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)
I looked at it but could not figure out where this error stems from.
I searched on google about this BulkCursorToCursorAdaptor class and I found that it throws this exception for different reasons for different situations.
I was wondering if you could explain where the error comes from because I am not closing the cursor in my adaptor.
Here are snippets of the code:
Activity with 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);
}
This is the adaptor:
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);
....
....
}
Please help