views:

230

answers:

2

I have an Observable and an Observer. The observable does download some stuff in a background thread and calls notifyObservers to let the observers read the status.

At some point in public void update the observer tries to updates the GUI

((TextView)findViewById('R.id.foo')).setText("bar");

but it seems like the observable thread calls this method, because the Observable (!!!) throws this:

 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
 at android.view.ViewRoot.checkThread(ViewRoot.java:2462)
 at android.view.ViewRoot.requestLayout(ViewRoot.java:512)
 ...
 at com.mynamespace.acitivty.TrackActivity.startPlay(TrackActivity.java:72)
 at com.mynamespace.acitivty.TrackActivity.update(TrackActivity.java:107)
 at java.util.Observable.notifyObservers(Observable.java:147)
 at java.util.Observable.notifyObservers(Observable.java:128)
 at com.mynamespace.module.communication.Download.stateChanged(Download.java:213)
 at com.mynamespace.module.communication.Download.run(Download.java:186)
 at java.lang.Thread.run(Thread.java:1058)

Is there some way I can prevent this from happening? I'm sure I'm missing something obvious here.

+2  A: 

I think your issue is similar to this one: http://stackoverflow.com/questions/1536654/androidandroid-view-viewrootcalledfromwrongthreadexception-how-to-solve-the-p

As bhatt4982 answers there, you can use Handler. From its doc:

"There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.".

The answer from AshtonBRSC is also good, you can enclose your UI update in a Runnable and use the runOnUiThread Activity's method.

Jala
A: 

I have the same problem.

  1. I have two threads 'worker' and 'UI' thread.

  2. worker keep on waiting for data from server, when gets it notifies to UI thread.

  3. On update UI Toast a msg on screen.

Step 3 is problem as it says

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

using mHandler, runOnUIThread slows down the UI thread (UI displays webview), as I have to continuously check for data from server.

Thanks in advance.

Placidnick