こんにちは、以下のコードを使用して GPS を有効にしようとしましたが、GPS を有効にしているときに NullPointerException が発生します
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
そして私のlogcatは
09-08 23:12:28.799: W/System.err(7222): java.lang.NullPointerException
09-08 23:12:28.799: W/System.err(7222): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
09-08 23:12:28.799: W/System.err(7222): at com.eheuristics.android.diegodeals.googleplacesandmaps.GPSTracker.turnGPSOn(GPSTracker.java:110)
09-08 23:12:28.799: W/System.err(7222): at com.eheuristics.android.diegodeals.googleplacesandmaps.GPSTracker.getLocation(GPSTracker.java:63)
09-08 23:12:28.799: W/System.err(7222): at com.eheuristics.android.diegodeals.googleplacesandmaps.GPSTracker.<init>(GPSTracker.java:45)
09-08 23:12:28.799: W/System.err(7222): at com.eheuristics.android.diegodeals.NearestLocation.onCreate(NearestLocation.java:79)
09-08 23:12:28.799: W/System.err(7222): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-08 23:12:28.799: W/System.err(7222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
09-08 23:12:28.799: W/System.err(7222): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
09-08 23:12:28.799: W/System.err(7222): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
09-08 23:12:28.799: W/System.err(7222): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
09-08 23:12:28.799: W/System.err(7222): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 23:12:28.799: W/System.err(7222): at android.os.Looper.loop(Looper.java:130)
09-08 23:12:28.799: W/System.err(7222): at android.app.ActivityThread.main(ActivityThread.java:3835)
09-08 23:12:28.799: W/System.err(7222): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 23:12:28.799: W/System.err(7222): at java.lang.reflect.Method.invoke(Method.java:507)
09-08 23:12:28.799: W/System.err(7222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
09-08 23:12:28.799: W/System.err(7222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
09-08 23:12:28.799: W/System.err(7222): at dalvik.system.NativeStart.main(Native Method)
の最初の行でエラーが発生しています