IllegalMonitorStateException Java Android

Runnable runner = new Runnable() {

    @Override
    public void run() {
        Movie m = adapter.getItem(id);
        m.setTitle(title);

        View v = listView.getChildAt(id);
        if (v != null) {
            ProgressBar p = (ProgressBar) v.findViewById(R.id.progressBar1);
            if (p != null)
                p.setVisibility(View.GONE);
        }

        adapter.notifyDataSetChanged();
        this.notify();
    }
};

synchronized (runner) {
    runOnUiThread(runner);

    try {
        runner.wait();

    } catch (InterruptedException e) {
    }

}

This code above causes a crash. I don't know why. It is supposed to run on the ui thread so i can disable the progressbars and update the movie information. Can anyone help me please?

Jon Skeet
people
quotationmark

I don't know why.

You didn't provide a stack trace, but I assume it's on the call to this.notify().

From the documentation of Object.notify():

Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.

In other words, you'd need:

synchronized (this) {
    this.notify();
}

Always look at the stack trace for an exception, and always consult the documentation to see if it exlpains why you're getting the exception.

people

See more on this question at Stackoverflow