我拉我的头发,试图找到一个很好的解决这个问题。我在我的应用程序,所以它找到我的位置。它似乎过于复杂,但很容易。但是下面的谷歌建议,我现在想运行此code作为一个异步任务,所以它不会占用用户界面。不过,我甚至不能弄清楚如何获得code编译没关系实际运行。该错误是,当我引用它删除听众locationManager不能得到解决。我已经使用了code贴在这里Android寻找GPS定位一次,显示加载对话框。然而,在这个例子中currentLocation并不似乎被引用到任何东西,我跑进试图理清了这一点问题。我已经浪费了将近一整天试图理清这个问题,所以我真的AP preciate如果有人能在正确的方向指向我。
私有类LocationControl扩展的AsyncTask<上下文,太虚,地点> {
公共场所alocation;
私人LocatoinManager locationManager;
@覆盖
公共场所doInBackground(上下文... PARAMS){
locationManager =(LocationManager)PARAMS [0] .getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,0,0,LocationListener的);
返回alocation;
}
公共LocationListener的LocationListener的=新LocationListener的(){
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
公共无效onProviderEnabled(字符串提供商){}
公共无效onProviderDisabled(字符串提供商){}
公共无效onLocationChanged(android.location.Location mlocation){
saveLocation(mlocation);
locationManager.removeUpdates(LocationListener的);
}
};
无效saveLocation(android.location.Location位置){
alocation =位置;
}
@覆盖
保护无效onPostExecute(地点结果){
useLocation(结果);
super.onPostExecute(结果);
}
@覆盖
在preExecute保护无效(){
dialog.show(WasserSportLotse.this,,查找位置...);
super.on preExecute();
}
}
于是,我改变了code与您的建议,它现在编译。我有更新code在这里重新present我运行的。我得到一个运行时错误时locationManager requestsLocationUpdates。这里是logcat的。任何想法?
04-15 14:57:56.742:ERROR / AndroidRuntime(18328):未捕获的处理程序:线程的AsyncTask#1退出,由于未捕获的异常
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):java.lang.RuntimeException的:执行doInBackground时出错()
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:234)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:258)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.util.concurrent.FutureTask.run(FutureTask.java:122)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.runTask(ThreadPoolExecutor.java:648)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:673)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.lang.Thread.run(Thread.java:1060)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):java.lang.RuntimeException的:所致。内螺纹已经不叫尺蠖prepare无法创建处理器()
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.os.Handler< INIT>(Handler.java:121)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.location.LocationManager $ ListenerTransport $ 1进行; INIT>(LocationManager.java:128)
。04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.location.LocationManager $ ListenerTransport< INIT>(LocationManager.java:126)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.location.LocationManager._requestLocationUpdates(LocationManager.java:697)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.location.LocationManager.requestLocationUpdates(LocationManager.java:619)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在com.WasserSportLotse.WasserSportLotse $ LocationControl.doInBackground(WasserSportLotse.java:62)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在com.WasserSportLotse.WasserSportLotse $ LocationControl.doInBackground(WasserSportLotse.java:1)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:256)
04-15 14:57:56.942:ERROR / AndroidRuntime(18328):4 ...更多
04-15 14:58:23.451:ERROR / AndroidRuntime(18356):未捕获的处理程序:线程的AsyncTask#1退出,由于未捕获的异常
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):java.lang.RuntimeException的:执行doInBackground时出错()
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:234)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:258)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.util.concurrent.FutureTask.run(FutureTask.java:122)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.runTask(ThreadPoolExecutor.java:648)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:673)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.lang.Thread.run(Thread.java:1060)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):java.lang.RuntimeException的:所致。内螺纹已经不叫尺蠖prepare无法创建处理器()
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.os.Handler< INIT>(Handler.java:121)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.location.LocationManager $ ListenerTransport $ 1进行; INIT>(LocationManager.java:128)
。04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.location.LocationManager $ ListenerTransport< INIT>(LocationManager.java:126)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.location.LocationManager._requestLocationUpdates(LocationManager.java:697)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.location.LocationManager.requestLocationUpdates(LocationManager.java:619)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在com.WasserSportLotse.WasserSportLotse $ LocationControl.doInBackground(WasserSportLotse.java:62)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在com.WasserSportLotse.WasserSportLotse $ LocationControl.doInBackground(WasserSportLotse.java:1)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:256)
04-15 14:58:23.642:ERROR / AndroidRuntime(18356):4 ...更多
![Android性能优化典范之多线程篇](/d/file/2023/09-17/3657557f3d4373791368f0699fab067a.png)
解决方案
公共类FastMainActivity延伸活动{
按钮searchBtn = NULL;
意图locatorService = NULL;
AlertDialog alertDialog = NULL;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
searchBtn =(按钮)findViewById(R.id.searchBtn);
searchBtn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
如果(!startService()){
CreateAlert(服务无法启动,错误!);
} 其他 {
Toast.makeText(FastMainActivity.this,服务启动,
Toast.LENGTH_LONG).show();
}
}
});
}
公共布尔stopService(){
如果(this.locatorService!= NULL){
this.locatorService = NULL;
}
返回true;
}
公共布尔startService(){
尝试 {
// this.locatorService =新
//意图(FastMainActivity.this,LocatorService.class);
// startService(this.locatorService);
FetchCordinates fetchCordinates =新FetchCordinates();
fetchCordinates.execute();
返回true;
}赶上(异常错误){
返回false;
}
}
公共AlertDialog CreateAlert(标题字符串,字符串消息){
AlertDialog警报=新AlertDialog.Builder(本).create();
alert.setTitle(职称);
alert.setMessage(消息);
返回警报;
}
公共类FetchCordinates扩展的AsyncTask<字符串,整数,字符串> {
ProgressDialog progDailog = NULL;
公共双拉提= 0.0;
公共双隆基= 0.0;
公共LocationManager mLocationManager;
公共VeggsterLocationListener mVeggsterLocationListener;
@覆盖
在preExecute保护无效(){
mVeggsterLocationListener =新VeggsterLocationListener();
mLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,0,0,
mVeggsterLocationListener);
progDailog =新ProgressDialog(FastMainActivity.this);
progDailog.setOnCancelListener(新OnCancelListener(){
@覆盖
公共无效OnCancel的(DialogInterface对话){
FetchCordinates.this.cancel(真正的);
}
});
progDailog.setMessage(载入中...);
progDailog.setIndeterminate(真正的);
progDailog.setCancelable(真正的);
progDailog.show();
}
@覆盖
保护无效onCancelled(){
的System.out.println(用户已取消!);
progDialog.dismiss();
mLocationManager.removeUpdates(mVeggsterLocationListener);
}
@覆盖
保护无效onPostExecute(字符串结果){
progDailog.dismiss();
Toast.makeText(FastMainActivity.this,
纬度:+ LATI +经度:+茇,
Toast.LENGTH_LONG).show();
}
@覆盖
保护字符串doInBackground(字符串... PARAMS){
// TODO自动生成方法存根
而(this.lati == 0.0){
}
返回null;
}
公共类VeggsterLocationListener实现LocationListener的{
@覆盖
公共无效onLocationChanged(位置定位){
INT纬度=(INT)location.getLatitude(); // * 1E6);
INT数=(INT)location.getLongitude(); // * 1E6);
INT ACC =(int)的(location.getAccuracy());
字符串信息= location.getProvider();
尝试 {
// LocatorService.myLatitude = location.getLatitude();
// LocatorService.myLongitude = location.getLongitude();
LATI = location.getLatitude();
隆基= location.getLongitude();
}赶上(例外五){
// progDailog.dismiss();
// Toast.makeText(getApplicationContext(),无法获取位置
//,Toast.LENGTH_LONG).show();
}
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
Log.i(OnProviderDisabled,OnProviderDisabled);
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
Log.i(onProviderEnabled,onProviderEnabled);
}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,
捆绑演员){
Log.i(onStatusChanged,onStatusChanged);
}
}
}
}
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();
}
}
So I changed the code with your recommendations and it now compiles. I have update the code on here to represent what I am running. I am getting a runtime error when the locationManager requestsLocationUpdates. Here is the logCat. Any Ideas?
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
解决方案
public class FastMainActivity extends Activity {
Button searchBtn = null;
Intent locatorService = null;
AlertDialog alertDialog = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchBtn = (Button) findViewById(R.id.searchBtn);
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!startService()) {
CreateAlert("Error!", "Service Cannot be started");
} else {
Toast.makeText(FastMainActivity.this, "Service Started",
Toast.LENGTH_LONG).show();
}
}
});
}
public boolean stopService() {
if (this.locatorService != null) {
this.locatorService = null;
}
return true;
}
public boolean startService() {
try {
// this.locatorService= new
// Intent(FastMainActivity.this,LocatorService.class);
// startService(this.locatorService);
FetchCordinates fetchCordinates = new FetchCordinates();
fetchCordinates.execute();
return true;
} catch (Exception error) {
return false;
}
}
public AlertDialog CreateAlert(String title, String message) {
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle(title);
alert.setMessage(message);
return alert;
}
public class FetchCordinates extends AsyncTask<String, Integer, String> {
ProgressDialog progDailog = null;
public double lati = 0.0;
public double longi = 0.0;
public LocationManager mLocationManager;
public VeggsterLocationListener mVeggsterLocationListener;
@Override
protected void onPreExecute() {
mVeggsterLocationListener = new VeggsterLocationListener();
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
mVeggsterLocationListener);
progDailog = new ProgressDialog(FastMainActivity.this);
progDailog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
FetchCordinates.this.cancel(true);
}
});
progDailog.setMessage("Loading...");
progDailog.setIndeterminate(true);
progDailog.setCancelable(true);
progDailog.show();
}
@Override
protected void onCancelled(){
System.out.println("Cancelled by user!");
progDialog.dismiss();
mLocationManager.removeUpdates(mVeggsterLocationListener);
}
@Override
protected void onPostExecute(String result) {
progDailog.dismiss();
Toast.makeText(FastMainActivity.this,
"LATITUDE :" + lati + " LONGITUDE :" + longi,
Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
while (this.lati == 0.0) {
}
return null;
}
public class VeggsterLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat = (int) location.getLatitude(); // * 1E6);
int log = (int) location.getLongitude(); // * 1E6);
int acc = (int) (location.getAccuracy());
String info = location.getProvider();
try {
// LocatorService.myLatitude=location.getLatitude();
// LocatorService.myLongitude=location.getLongitude();
lati = location.getLatitude();
longi = location.getLongitude();
} catch (Exception e) {
// progDailog.dismiss();
// Toast.makeText(getApplicationContext(),"Unable to get Location"
// , Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
Log.i("OnProviderDisabled", "OnProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.i("onProviderEnabled", "onProviderEnabled");
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
Log.i("onStatusChanged", "onStatusChanged");
}
}
}
}
相关推荐
最新文章