开发者

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 origi开发者_如何学JAVAnal 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();


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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜