-1

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

kenju
  • 5,866
  • 1
  • 41
  • 41
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

1 Answers1

0

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.
}
Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Ya i can check it, it is not an issue but how to start GPS that's the main issue i can not turn it on if i tried with above code i got null pointer exception – Siddhpura Amit Sep 09 '12 at 04:20
  • 1
    For security reasons, an app cannot turn on or off the GPS settings, but you can open the settings menu so that the user can change this setting. – Sam Sep 09 '12 at 16:21