0

Here's what I'm trying to do first. I'm trying to launch a MapView, which grabs the user's location and loads an overlay with data that changes based upon the user's location.

Code overview: I have a MapView that grabs the current user's location on creation. The locationOverlay I'm using calls out a Runnable on first fix:

locationOverlay.runOnFirstFix(centerAroundFix);

This Runnable, centerAroundFix, starts an aSyncTask. As soon as I call the 'new aSyncTask', this error is thrown:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

The asynctask's job is to fetch data, create the overlay, and add the overlay. Creating and executing the asynctask works fine outside of the Runnable.

Jeff Lamb
  • 5,755
  • 4
  • 37
  • 54

2 Answers2

0

Not sure how I didn't see this already...

Can't create handler inside thread which has not called Looper.prepare()

I'm pretty sure this is a duplicate question.

Edit: works. Here's my code. Wrapped the updateLocation() call with "mHandler.post":

private Handler mHandler = new Handler(Looper.getMainLooper());

private Runnable centerAroundFix = new Runnable() {
    public void run() {
        /* Jump to current location and set zoom accordingly */
        map.getController().animateTo(locationOverlay.getMyLocation());
        map.getController().setZoom(12);
        /* This is a different thread.  I need to call this from a thread that has a 'Looper' prepared. */
        mHandler.post(new Runnable() {
            public void run() {
                updateLocation();
            }
        });
    }
};
Community
  • 1
  • 1
Jeff Lamb
  • 5,755
  • 4
  • 37
  • 54
0

You have to create the Handler in the UIThread. For example as class member:

public class Activity extends Activity{
    private Handler handler = new Handler();        
}
thaussma
  • 9,756
  • 5
  • 44
  • 46