Hi,

The original code that you posted did contain concurrent (multithreaded) reads and writes of the activity member variable. That concurrency was causing null pointed exceptions in a *seemingly* impossible place. The reason, as far as I can tell, you had concurrent access to the member variable was that you wanted to delegate the actual background work to the activity, while keeping your async task implementation generic and unaware of the exact nature of background work.

So, with this in mind, I can see two ways of fixing things:

1) Keeping your way of structuring the code, and adding proper thread synchronization.

Here is one way:

void detach() {
    synchronized(this) {
        this.activity = null
    }
}

... elsewhere (IIRC)...

void the_method_where_you_were_getting_a_null_pointer_exception() {
        MyActivityClass checkedActivity;
        synchronized(this) {
            checkedActivity = this.activity;
        }
        if (checkedActivity != null) {
            .....
        }
}

There are other things to be careful with here, for example, the activity may be paused, stopped, or its onDestroy called (on the UI thread) while the async task's thread is still in its worker method. Accesses to member variables of activity would also need to be synchronized, or preferably avoided.... So don't undertake this unless you've learned at least the basics of concurrent programming.

2) Restructuring the code in a more.... orthodox way, as described in the docs and was already suggested on this thread by Streets of Boston and TreKing (IIRC).

Have your AsyncTask perform the work without delegating to the activity, and only reference the activity (if any) on the UI thread, in onPostExecute. This way you'll be avoiding concurrent reads/writes of the activity member variable, because onPostExecute and detach are both executed on the UI thread.

Hope this helps.

-- Kostya

19.11.2011 7:40, Bluemercury ?????:
Hi there! Sorry for posting here again, but i was hoping if i could try this sample, you said to put the synchronized in the detach method here:

void detach() {
*activity=null;*
}

in the asynctask, is this correct?but detach() is only called in the activity and not the in the async task itself, i thought synchronized methods assumed that they're both launched from 2 different threads....but in this case the activity(UI thread) calls the detach method like you said, but the async task doesnt, only the attach method...
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to