I am pulling my hair out with trying to find a good solution to this problem. I made my app so it finds my location. It seems overly complicated but easy enough. However following googles recommendations I am now trying to run this code as an Async task so it doesn t tie up the UI. However I can t even figure out how to get the code to compile never mind actually run. The error is that locationManager cannot be resolved when I reference it to remove the listeners. I have tried using the code posted on here Android find GPS location once, show loading dialog. However in this example currentLocation doesn t seemed to be referenced to anything and I ran into problems trying to sort that out. I have wasted nearly a full day trying to sort this problem so I would really appreciate if somebody can point me in the right direction.
private class LocationControl extends AsyncTask<Context, Void, Location> {
public Location alocation;
private LocatoinManager locationManager;
@Override
public Location doInBackground(Context... params) {
locationManager = (LocationManager) params[0].getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, locationListener);
return alocation;
}
public LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
public void onLocationChanged(android.location.Location mlocation) {
saveLocation(mlocation);
locationManager.removeUpdates(locationListener);
}
};
void saveLocation(android.location.Location location){
alocation = location;
}
@Override
protected void onPostExecute(Location result) {
useLocation(result);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
dialog.show(WasserSportLotse.this, " ", "Finding location...");
super.onPreExecute();
}
}
因此,我根据你的建议修改了该守则,现在该法典汇编成册。 我在此更新了法典,以代表我正在做的事情。 当Manager要求搬迁时,我正遇到一个错误。 地点更新。 这里是标志。 任何想法?
04-15 14:57:56.742: ERROR/AndroidRuntime(18328): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): java.lang.RuntimeException: An error occured while executing doInBackground()
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.lang.Thread.run(Thread.java:1060)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): Caused by: java.lang.RuntimeException: Can t create handler inside thread that has not called Looper.prepare()
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.os.Handler.<init>(Handler.java:121)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:128)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:126)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:697)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:619)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at com.WasserSportLotse.WasserSportLotse$LocationControl.doInBackground(WasserSportLotse.java:62)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at com.WasserSportLotse.WasserSportLotse$LocationControl.doInBackground(WasserSportLotse.java:1)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
04-15 14:57:56.942: ERROR/AndroidRuntime(18328): ... 4 more
04-15 14:58:23.451: ERROR/AndroidRuntime(18356): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): java.lang.RuntimeException: An error occured while executing doInBackground()
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.lang.Thread.run(Thread.java:1060)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): Caused by: java.lang.RuntimeException: Can t create handler inside thread that has not called Looper.prepare()
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.os.Handler.<init>(Handler.java:121)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:128)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:126)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:697)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:619)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at com.WasserSportLotse.WasserSportLotse$LocationControl.doInBackground(WasserSportLotse.java:62)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at com.WasserSportLotse.WasserSportLotse$LocationControl.doInBackground(WasserSportLotse.java:1)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
04-15 14:58:23.642: ERROR/AndroidRuntime(18356): ... 4 more