I have a TimerTask that is causing my app to crash when I go to a different Intent. Here is the error:
06-06 02:45:54.884: E/AndroidRuntime(602): FATAL EXCEPTION: main
06-06 02:45:54.884: E/AndroidRuntime(602): android.database.StaleDataException: Access closed cursor
06-06 02:45:54.884: E/AndroidRuntime(602): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
06-06 02:45:54.884: E/AndroidRuntime(602): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
06-06 02:45:54.884: E/AndroidRuntime(602): at com.myapp.ExerciseActivity$TickClass$1.run(ExerciseActivity.java:221)
06-06 02:45:54.884: E/AndroidRuntime(602): at android.os.Handler.handleCallback(Handler.java:587)
06-06 02:45:54.884: E/AndroidRuntime(602): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 02:45:54.884: E/AndroidRuntime(602): at android.os.Looper.loop(Looper.java:123)
06-06 02:45:54.884: E/AndroidRuntime(602): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-06 02:45:54.884: E/AndroidRuntime(602): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 02:45:54.884: E/AndroidRuntime(602): at java.lang.reflect.Method.invoke(Method.java:507)
06-06 02:45:54.884: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-06 02:45:54.884: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-06 02:45:54.884: E/AndroidRuntime(602): at dalvik.system.NativeStart.main(Native Method)
Now here is the code for the TimerTask:
public class TickClass extends TimerTask
{
private int columnIndex;
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (cursor != null) {
if (_index == 1) {
columnIndex = cursor.getColumnIndex(MySQLiteHelper.COLUMN_IMAGE_2);
_index = 2;
}
else {
columnIndex = cursor.getColumnIndex(MySQLiteHelper.COLUMN_IMAGE_1);
_index = 1;
}
String image_1 = cursor.getString(columnIndex);
image_1 = image_1.replace(".png", "");
int resourceId = getResources().getIdentifier(getPackageName() + ":drawable/" + image_1, null, null);
image_1_view.setImageDrawable(getResources().getDrawable(resourceId));
}
}
});
}
}
All it does is animate a PNG.
Notice how I have added the if (cursor != null)
conditional statement, which didn't fix it. I am not sure how to fix this, is it because it is running on the UI thread, so even though I've navigated away from the Activity, it is still running, but the cursor no longer exists since it is tied in with the Activity?
EDIT:
I've fixed the force close by cancel()ing the TimerTask onPause(). However, now when I return to the Activity, the image is no longer animated. Any thoughts?