Java Thread Message Passing

Posted by pkulak on Stack Overflow See other posts from Stack Overflow or by pkulak
Published on 2010-06-16T05:37:42Z Indexed on 2010/06/16 5:42 UTC
Read the original article Hit count: 186

Filed under:
|

I'm writing an Android app. I have a main method, which creates and runs a new Thread using an anonymous inner Runnable class. The run() method, when it's done, calls a method on it's parent class (in the main thread) that calls notifyDataSetChanged() so that the main thread can redraw the new data. This is causing all kinds of trouble (ViewRoot$CalledFromWrongThreadException).

The thing is, this method being called from the worker thread is on the class that's created in the UI thread. Shouldn't that be running on the UI thread? Or am I missing something?

Here's some code about what I'm talking about:

public class Mealfire extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        (new Thread() {
            public void run() {
                // Do a bunch of slow network stuff.
                update();
            }
        }).start();
    }

    private void update() {
        myAdapter.notifyDatasetChanged();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about android