您好,我首先检查 LocationManager 是否启用了 GPS
LocationManager locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
我已经使用了许可
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
现在如果没有启用 GPS,我打开一个对话框说你要启用 GPS?
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
但我在我的 logcat 中得到空指针异常如下
09-09 09:46:04.599: W/System.err(935): java.lang.NullPointerException
09-09 09:46:04.599: W/System.err(935): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:363)
09-09 09:46:04.599: W/System.err(935): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:742)
09-09 09:46:04.599: W/System.err(935): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:283)
09-09 09:46:04.599: W/System.err(935): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:274)
09-09 09:46:04.599: W/System.err(935): at com.eheuristics.android.diegodeals.googleplacesandmaps.GPSTracker.turnGPSOn(GPSTracker.java:110)
09-09 09:46:04.599: W/System.err(935): at com.eheuristics.android.diegodeals.googleplacesandmaps.GPSTracker.getLocation(GPSTracker.java:63)
09-09 09:46:04.599: W/System.err(935): at com.eheuristics.android.diegodeals.googleplacesandmaps.GPSTracker.<init>(GPSTracker.java:45)
09-09 09:46:04.599: W/System.err(935): at com.eheuristics.android.diegodeals.NearestLocation.onCreate(NearestLocation.java:79)
09-09 09:46:04.599: W/System.err(935): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 09:46:04.599: W/System.err(935): at android app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
09-09 09:46:04.599: W/System.err(935): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
09-09 09:46:04.599: W/System.err(935): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
09-09 09:46:04.599: W/System.err(935): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
09-09 09:46:04.599: W/System.err(935): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 09:46:04.599: W/System.err(935): at android.os.Looper.loop(Looper.java:130)
09-09 09:46:04.609: W/System.err(935): at android.app.ActivityThread.main(ActivityThread.java:3835)
09-09 09:46:04.609: W/System.err(935): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 09:46:04.609: W/System.err(935): at java.lang.reflect.Method.invoke(Method.java:507)
09-09 09:46:04.609: W/System.err(935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)