I want to update a variable in my activity once every one second and for that i am using an Async task and a runnable inside it. I keep getting this error saying “Cannot refer to a non-final variable myRunnable inside an inner class defined in a different method” . When i change the declaration to final , it shows “The local variable myRunnable may not have been initialized” . How do i rectify the error? Is there a better way of doing what i want to do?
import android.os.AsyncTask;
import android.os.Handler;
public class Timer extends AsyncTask<String, Process, Void>{
static int seconds=0;
TimerTask timer;
static int minutes=0;
static int hours=0;
private String timeTaken;
public Handler myhandler = new Handler();
@Override
protected Void doInBackground(String... params) {
final Runnable myRunnable = new Runnable() {
@Override
public void run() {
......
......
......
myhandler.postDelayed(myRunnable, 1000);
}
};
return null;
}
How about something like this instead of using an AsyncTask (although something similar can be done within an AsyncTask, and because it runs in another thread I could simply block this worker thread using Thread.sleep(…) instead of doing the whole Runnable thing…