user1534735
user1534735

Reputation:

Cannot refer to a non-final variable inside an inner class defined in different method

new Thread(new Runnable() 
                {
                    public void run() {
                        while ( statusStr==null)
                        {
                            progressBarHandler.post(new Runnable() {
                                public void run() {
                                    progressBar.setProgress(progressBarStatus);
                                }
                            });
                        }

                    }
                    }).start();

Friends,I have referred other question but I am still confused so please help me in solving this issue. I cannot keep statusStr as final coz its values are updating while the app is working so please give me a solution.

Upvotes: 0

Views: 2092

Answers (2)

jeet
jeet

Reputation: 29199

Either declare progressBar as final or declare it as a GlobalVariable.

Like declaring final variable do as follows:

final ProgressBar progressBar= ProgressBar.show(this, "title", "message");
    new Thread(new Runnable() 
                    {
                        public void run() {
                            while ( statusStr==null)
                            {
                                progressBarHandler.post(new Runnable() {
                                    public void run() {
                                        progressBar.setProgress(progressBarStatus);
                                    }
                                });
                            }

                        }
                        }).start();

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

You can declare the variable globally so that you will not be forced to change it to final.

Upvotes: 3

Related Questions