From my previous question
I was getting NullPointor exception. So i have done some changes in my code and now progress bar is showing up but getting the below error
Can't create handler inside thread that has not called Looper.prepare()
You are accessing something on the UI thread from inside doInBackground. And that's not allowed.
*edit: I've seen everything. To me, what looks mostly unusual are those lines:
updateWithNewLocation(location); // What does this do? Could you tell me?
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener); // I don't think this is it
Try to move all those location update stuff into the onProgressUpdate
. I'm blind here since I can't see your code, but I'm guessing it does something with the UI. Try this:
@Override
protected void onProgressUpdate(Location... locations) {
super.onProgressUpdate(values);
updateWithNewLocation(location);
}
But as I said, I'm not sure since I can't know what updateWithNewLocation
does unless you post that code here.
You're doing a bunch of stuff in your doInBackground()
that you don't need to do in there. Only do the stuff in your doInBackground()
method that would cause the stuttering on the UI thread. In other words, all the code that doesn't directly deal with your network communication needs to be moved out of your doInBackground()
method.
This is exactly why it's valuable to start understanding some basics around threading instead of just saying "AsyncTask" for everything that seems remotely threading-related in Android. Multi-threading can be tricky and you have to think things through.
I suspect this is what's happening: You have an AsyncTask which among other things gets the location... asynchronously. That means you start on the main thread, you execute doInBackground
in another thread implicitly (the AsyncTask's thread), then you call off to whatever thread is doing your location acquisition. Then your AsyncTask thread continues on, finishing off all that JSON-related work in doInBackground
, probably finishing. The AsyncTask thread likely finishes. Then you are getting your location lock, and the listener that you provided is called back, except that now the AsyncTask has already finished, and its thread is no longer valid.
Just based on a look at your code I suspect that you want to make that Google Maps API call after you get your location, right? If so, call your "get location" code synchronously. In the callback to that, you can put the code to kick off your AsyncTask which now only does the Google Maps API call and processing.