dev_android
dev_android

Reputation: 8818

Android change in view depending upon a thread is over

I have created a Thread in onCreate(). It is running fine. Now I want to change something in view, when the thread is over. But if add it in the run() method, it is giving android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. error. How to solve this problem?

Thread updateThread = new Thread() {
 @Override
 public void run() {
  ................
  ................
   //this line is giving problem
loader.setVisibility(View.INVISIBLE);                               
 }
};
updateThread.start();

Upvotes: 0

Views: 1180

Answers (2)

Lukas Knuth
Lukas Knuth

Reputation: 25755

In Android, only the original UI-Thread can make changes to it's View. That's security policy. A solution that might be interesting for you, too is shown in this older thread.

Also have a look at Android's Handler-class (which is used in the Thread above).

But (as mentioned by zerpage), an AsyncTask might be the better way.

Upvotes: 1

You should use an AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html instead of Thread, and do your loader.setVisibility(View.INVISIBLE) inside onPostExecute()

Upvotes: 1

Related Questions