'Enable GPS null pointer exception
Hi i have tried to enable GPS for that i am using below code but i am getting NullPointerException while enabling GPS
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);
}
}
and my logcat is
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)
i am getting error on the first line in
Solution 1:[1]
Settings.Secure.getString() will return null if Settings.Secure.LOCATION_PROVIDERS_ALLOWED is not in its database.
From your context, if provider contains "gps" then the GPS is enabled. So if everything is disabled then provider might be null...
Try adding:
if(provider == null || !provider.contains("gps")) {
Since you are probably using the LocationManager to retrieve GPS coordinates, consider using the isProviderEnabled() method. There is an example here: How do I find out if the GPS of an Android device is enabled
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// Open Location Permissions Settings
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_REQUEST_CODE);
// Override onActivityResult() to verify that the user has turned the GPS on.
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
