如果在启动LocationUpdates之前关闭gps,我尝试创建一个配置gps的对话框,但是我有以下错误。 任何解决方案
这是MainActivity
public class MainActivity extends Activity {
LocationManager lm;
LocationListener locationListener;
Context context;
private TextView mLatLng;
DialogSettingsGPS dialogGPS;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---use the LocationManager class to obtain locations data---
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLatLng = (TextView) findViewById(R.id.lat_lng);
dialogGPS = new DialogSettingsGPS(MainActivity.this);
// getting GPS status
isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isGPSEnabled){
locationListener = new MyLocationListener();
}else{
dialogGPS.showSettingsAlert();
}
}
@Override
public void onResume() {
super.onResume();
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
10000, //mínimo tiempo de actualización
0, //mínima distancia de act.
locationListener);
}
@Override
public void onDestroy() {
super.onDestroy();
//---remove the location listener---
lm.removeUpdates(locationListener);
}
//Para escuchar los cambios el Location, se necesita crear una clase
//que implemente la interfaz de LocationListener.
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
mLatLng.setText(getString(R.string.latitude_longitude, latitude, longitude));
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
这是对话框的类,DialogSettingsGPS:
public class DialogSettingsGPS extends Service {
private final Context mContext;
public DialogSettingsGPS(Context context){
this.mContext = context;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("Configuración GPS");
// Setting Dialog Message
alertDialog.setMessage("GPS desactivado. ¿Desea ir al menu de configuración?");
// On pressing Settings button
alertDialog.setPositiveButton("Configuración", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
这是错误:
03-20 16:33:10.643:E / AndroidRuntime(6750):致命异常:主要 03-20 16:33:10.643:E / AndroidRuntime(6750):流程: com.example.followtrackgps,PID:6750 03-20 16:33:10.643: E / AndroidRuntime(6750):java.lang.RuntimeException:无法恢复 活动 {com.example.followtrackgps / com.example.followtrackgps.MainActivity}: java.lang.IllegalArgumentException:无效的侦听器:null 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2826) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.ActivityThread.access $ 800(ActivityThread.java:139)03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1210) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.os.Handler.dispatchMessage(Handler.java:102)03-20 16:33:10.643:E / AndroidRuntime(6750):at android.os.Looper.loop(Looper.java:136)03-20 16:33:10.643: E / AndroidRuntime(6750):at android.app.ActivityThread.main(ActivityThread.java:5102)03-20 16:33:10.643:E / AndroidRuntime(6750):at java.lang.reflect.Method.invokeNative(Native Method)03-20 16:33:10.643:E / AndroidRuntime(6750):at java.lang.reflect.Method.invoke(Method.java:515)03-20 16:33:10.643: E / AndroidRuntime(6750):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 03-20 16:33:10.643:E / AndroidRuntime(6750):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)03-20 16:33:10.643:E / AndroidRuntime(6750):at dalvik.system.NativeStart.main(Native Method)03-20 16:33:10.643: E / AndroidRuntime(6750):引起:java.lang.IllegalArgumentException: 无效的监听器:null 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.location.LocationManager.checkListener(LocationManager.java:1635) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.location.LocationManager.requestLocationUpdates(LocationManager.java:450) 03-20 16:33:10.643:E / AndroidRuntime(6750):at com.example.followtrackgps.MainActivity.onResume(MainActivity.java:68) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1215) 03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.Activity.performResume(Activity.java:5327)03-20 16:33:10.643:E / AndroidRuntime(6750):at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2811) 03-20 16:33:10.643:E / AndroidRuntime(6750):... 12更多
提前感谢。