如果有任何所需的位置设置,我使用Google Play服务的新Location Settings API来显示对话框。
在包含片段的Activity内部,片段本身包含一个SupportMapFragment我使用此代码检查位置设置:
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(LOCATION_UPDATES_INTERVAL_MILLIS);
mLocationRequest.setFastestInterval(LOCATION_FASTEST_UPDATE_INTERVAL_MILLIS);
mLocationRequest.setSmallestDisplacement(LOCATION_SMALLES_DISPLACEMENT_METERS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationServices.SettingsApi.checkLocationSettings(GeofencingService.getGoogleApiClient(), builder.build()).setResultCallback(this);
在ResultCallback中,我显示如下对话框:
@Override
public void onResult(LocationSettingsResult result) {
switch (result.getStatus().getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
result.getStatus().startResolutionForResult(this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
throw new RuntimeException(e);
}
break;
...
}
}
我第一次访问该Activity时效果很好。但是,如果我访问其他特定活动(有些人甚至不显示任何地图),然后在此之后的任何时间访问第一个Activity,startResolutionForResult()会与此日志崩溃,这是一个奇怪的问题:
03-24 11:52:37.485 20424-20424/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.google.android.gms.ui, PID: 20424
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gms/com.google.android.location.settings.LocationSettingsCheckerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.location.LocationSettingsStates com.google.android.location.internal.n.b()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.location.LocationSettingsStates com.google.android.location.internal.n.b()' on a null object reference
at com.google.android.location.settings.LocationSettingsCheckerActivity.a(SourceFile:401)
at com.google.android.location.settings.LocationSettingsCheckerActivity.onCreate(SourceFile:142)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
在崩溃之后startResolutionForResult()然后每次都崩溃。唯一的解决方法是完全关闭应用程序并重新打开它。那是一个错误吗?有没有人体验过它?我能做些什么才能让它发挥作用?