从授予位置权限返回太快无法找到位置

时间:2016-08-30 12:39:51

标签: android gps locationmanager locationlistener

我开发了一个需要GPS定位的应用程序而且我得到了它,但是如果没有启用该位置,我会将用户重定向到设置以启用它。

一切正常,如果我不快速,但如果设置菜单打开时我激活它并立即返回到应用程序,它会导致空位置。

是否可以“等待应用获取该位​​置?”

修改

08-30 14:48:35.094 6878-6878/d.g.movida E/AndroidRuntime: FATAL EXCEPTION: main
                                                      Process: d.g.movida, PID: 6878
                                                      java.lang.RuntimeException: Unable to resume activity {d.g.movida/d.g.movida.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
                                                          at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4156)
                                                          at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                          at android.os.Looper.loop(Looper.java:158)
                                                          at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
                                                          at d.g.movida.MainActivity.updateList(MainActivity.java:121)
                                                          at d.g.movida.MainActivity.onResume(MainActivity.java:342)
                                                          at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1286)
                                                          at android.app.Activity.performResume(Activity.java:6987)
                                                          at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4145)
                                                          at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250) 
                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839) 
                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                          at android.os.Looper.loop(Looper.java:158) 
                                                          at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我在检查位置之前检查网络/ gps是否已启用,它是。

然后在获取之前检查位置是否为空(禁用以获取此错误)。

1 个答案:

答案 0 :(得分:0)

以下是我用来解决此问题的代码。它必须在单独的函数中调用,这将保证在对其进行调用之前对其进行初始化。

例如,我的应用使用谷歌地图,并在onMapReady回调中调用此函数。

这是一种更好的方式,可以要求启用位置,而不是将用户指向设置活动。

public void requestLocationPermissions() {

    mGoogleApiClient = new GoogleApiClient.Builder(getContext())
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    mGoogleApiClient.connect();

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

        @Override
        public void onResult(LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i(TAG, "All location settings are satisfied.");
                    //startLocationUpdates();
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                            "upgrade location settings ");

                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        status.startResolutionForResult(getActivity(), 1000);

                    } catch (IntentSender.SendIntentException e) {
                        Log.i(TAG, "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                            "not created.");
                    break;
            }
        }
    });
}