I originally used ListActivity. It was controlled using the CursorAdapter.
I tried the onScroll method to update the user interface element based on what is visible to the user when scrolling through the ListView. My onScroll method calls the adapter cursor to check some data about the first visible item.
I decided to place another ListView on the screen, so I changed it to Activity (instead of ListActivity), given ListView a + id, and created a member variable for it in Activity. I changed all references to getListView () to this variable.
This works fine, except for one thing: if you click an item in the list, it will start a new action. After this new action appears on the screen, the onScroll method is called for the previous action. This is a crashing application.
It is strange because the first two lines in my overrode onScroll make sure that the cursor is not zero, it is open, has a number> 0 and moves to a visible position. These two lines work fine. However, when I try to extract data from it, the application crashes with the following error:
01-12 13:22:47.752: ERROR/AndroidRuntime(526): Uncaught handler: thread main exiting due to uncaught exception
01-12 13:22:47.922: ERROR/AndroidRuntime(526): android.database.StaleDataException: Access closed cursor
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at com.myapp.app.activity.ActivityPrepare$4.onScroll(ActivityPrepare.java:160)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:656)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.ListView.layoutChildren(ListView.java:1429)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.view.View.layout(View.java:6830)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.view.View.layout(View.java:6830)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.view.View.layout(View.java:6830)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.view.View.layout(View.java:6830)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.os.Looper.loop(Looper.java:123)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at java.lang.reflect.Method.invoke(Method.java:521)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-12 13:22:47.922: ERROR/AndroidRuntime(526): at dalvik.system.NativeStart.main(Native Method)
I suppose there is some kind of race condition, but why is the onScroll call called when there is no activity even on the screen?
Any ideas on how to get around this?
source
share