Android:将progressdialog添加到我的当前位置查找程序崩溃。需要一次定位吗
地点类别:Android:将progressdialog添加到我的当前位置查找程序崩溃。需要一次定位吗,android,Android,地点类别: public class MyLocationListener implements LocationListener{ private Context cnt; private ProgressDialog d; public MyLocationListener(Context cnt){ this.cnt = cnt; this.d.show(cnt, "", "Finding your locat
public class MyLocationListener implements LocationListener{
private Context cnt;
private ProgressDialog d;
public MyLocationListener(Context cnt){
this.cnt = cnt;
this.d.show(cnt, "", "Finding your location...");
}
@Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
this.d.dismiss();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}/* End of Class MyLocationListener */
我就是这样实施的:
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener(cnt);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
当我添加进度对话框时,我得到一个空指针异常错误。当我把它拿出来的时候,一切都很好
进度对话框是否与Toast消息冲突
编辑:
看起来onLocationChanged经常更改,所以它最终会关闭一个已经关闭的对话框
有没有办法只找到一次位置?使用布尔值,但这不是最佳做法。 在您的情况下,在关闭progressDialog之前,您需要添加一个if:
if (this.d.isShowing())
this.d.dismiss();