开发者

Having a problem updating UI from a second thread where the first thread works just fine

I have a program where I am going to a website, grabbing XML and parsing, pretty simple.

Here is the code that works just fine, it's started in the onCreate method

final Handler handler = new Handler(){

 public void handleMessage(Message msg){
        progDialog.dismiss();
        parse(xmlOut);           
  }};

progDialog = ProgressDialog.show(this, "Working...", "Doing something..."); thread = new Thread() {

 public void run(){

xmlOut = doSomeFunction(todaysDate);
handler.sendEmptyMessage(0);

 }};

thread.start();

I then have a button on the page that reloads the d开发者_如何学Goata if a user chooses a different date, can't re-run the same thread, so I just copied the exact same code, changed all the variable names and put it int the OnClickListener for the Button.

When I click the button I get an exception and get this

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Doing something wrong but not sure what exactly. I dont' write too many threaded programs so I am sure I am missing something.


All the UI changes should be done from the same thread or a handler in that thread or from an AsyncTask. Check this post to read a little about AsyncTasks in android.

I am guessing that you did not do the Button declaration right or you are setting the event from another thread. That will give you this error. If you still are struck, then post the relevant code here and I can try to help you better.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜