您好我首先使用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)
答案 0 :(得分:1)
尝试使用getApplicationContext()而不是mContext。