I am working on an android application.In my activity I am using the following code.
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
//Got the location!
Drawable marker = getResources().getDrawable(
R.drawable.currentlocationmarker);//android.R.drawable.btn_star_big_on
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(marker);
currentmarkerPoint = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
currLocation = location;
mBlippcoordinate = currentmarkerPoint;
mBlippLocation = location;
myItemizedOverlay.addItem(currentmarkerPoint, "", "");
mBlippmapview.getOverlays().add(myItemizedOverlay);
animateToCurrentLocation(currentmarkerPoint);
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
I am using the above code to find location from gps or network .The animateToCurrentLocation(currentmarkerPoint); method contains a asynctask .So I am getting
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Thanks in advance.
You get this error when you are trying to create and run an AsyncTask from a thread that has no Looper attached to it. The AsyncTasks needs a Looper to post its “task completed” message back on the thread that started the AsyncTask.
Now your real question: how to get a thread with a Looper? It turns out you already have one: the main thread. As the documentation also states, you should create and .execute() your AsyncTask from the main thread. The doInBackground() will then run on a worker thread (from the AsyncTask threadpool) and you can access the network there. The onPostExecute() will then be run on you main thread, after it has been posted there via the main thread’s Handler/Looper.