-1

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

我在第一行遇到错误

4

1 回答 1

0

Settings.Secure.getString()Settings.Secure.LOCATION_PROVIDERS_ALLOWED如果不在其数据库中,将返回 null 。

根据您的上下文,如果provider包含"gps",则启用 GPS。因此,如果一切都被禁用,那么provider可能null......

尝试添加:

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

由于您可能正在使用 LocationManager 来检索 GPS 坐标,因此请考虑使用该isProviderEnabled()方法。这里有一个例子:How do I find if an Android device of an GPS 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.
}
于 2012-09-08T19:08:39.997 回答