Loren3737
Loren3737

Reputation: 11

Runnable Initialization Error

I am trying to do create an android application that updates an image based on information that has been received. In the onCreate method of my main activity I am trying to use the following code to constantly update as the app runs. Currently there is an error with the runnable I have marked below (bolded or with **). It says, "runnable may not have been initialized". I was wondering how to to fix this.

final int delayMs = 1000;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
    @Override public void run() {
        // Call your refresh method
        Refresher();
        // Restart the timer
        handler.postDelayed(**runnable**, delayMs);
    }
};
handler.postDelayed(runnable, delayMs);

Upvotes: 1

Views: 503

Answers (2)

Alex Tenishev
Alex Tenishev

Reputation: 1

It is not clear what is it you what to achieve by this? Could you provide more code here? Answering your exact question here, you are trying to use reference to your final 'runnable' variable inside Runnable implementation and it isn't clear that 'runnable' was initialized on time of use.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317467

Define runnable as a member of the enclosing class instead of as a local variable inside the same method where it's used.

Upvotes: 1

Related Questions