启用GPS空指针异常

时间:2012-09-08 18:52:44

标签: android android-intent

您好我已经尝试启用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)

我在

的第一行收到错误

1 个答案:

答案 0 :(得分:0)

如果Settings.Secure.LOCATION_PROVIDERS_ALLOWED不在其数据库中,

Settings.Secure.getString()将返回null。

在您的上下文中,如果provider包含"gps",则启用GPS。因此,如果所有内容都已停用,那么provider可能是null ...

尝试添加:

if(provider == null || !provider.contains("gps")) {

由于您可能正在使用LocationManager来检索GPS坐标,因此请考虑使用isProviderEnabled()方法。这里有一个例子: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.
}