2017-03-22 14 views
1

サンプルソースコードをhttps://github.com/googlemaps/android-samples/tree/master/tutorials/CurrentPlaceDetailsOnMapからダウンロードしました。許可の無限ループをリクエスト

これは、2つの関数の無限ループによって引き起こさ初めての実行時にクラッシュした:

private void updateLocationUI() { 
     if (mMap == null) { 
      return; 
     } 

     /* 
     * Request location permission, so that we can get the location of the 
     * device. The result of the permission request is handled by a callback, 
     * onRequestPermissionsResult. 
     */ 
     if (ContextCompat.checkSelfPermission(this.getApplicationContext(), 
       android.Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      mLocationPermissionGranted = true; 
     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
        PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 
     } 

     if (mLocationPermissionGranted) { 
      mMap.setMyLocationEnabled(true); 
      mMap.getUiSettings().setMyLocationButtonEnabled(true); 
     } else { 
      mMap.setMyLocationEnabled(false); 
      mMap.getUiSettings().setMyLocationButtonEnabled(false); 
      mLastKnownLocation = null; 
     } 
    } 

@Override 
    public void onRequestPermissionsResult(int requestCode, 
              @NonNull String permissions[], 
              @NonNull int[] grantResults) { 
     mLocationPermissionGranted = false; 
     switch (requestCode) { 
      case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        mLocationPermissionGranted = true; 
       } 
      } 
     } 
     updateLocationUI(); 
    } 

し、エミュレータ上でそれを実行します。APIのlvl25を、Androd 7.1.1

ログ:

03-22 23:34:18.249 24660-24660/com.example.mypc.mapdemo W/Activity: Can reqeust only one set of permissions at a time 
03-22 23:34:19.759 24660-24660/com.example.mypc.mapdemo W/Activity: Can reqeust only one set of permissions at a time 
03-22 23:34:20.501 24660-24660/com.example.mypc.mapdemo W/Activity: Can reqeust only one set of permissions at a time 
03-22 23:34:20.894 24660-24660/com.example.mypc.mapdemo W/Activity: Can reqeust only one set of permissions at a time 

クラッシュログ:

E/AndroidRuntime:  at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:372) 
                 at com.example.mypc.mapdemo.MainActivity.updateLocationUI(MainActivity.java:330) 
                 at com.example.mypc.mapdemo.MainActivity.onRequestPermissionsResult(MainActivity.java:207) 
                 at android.app.Activity.requestPermissions(Activity.java:4116) 
                 at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompatApi23.java:49) 
                 at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:37 
```2) 

質問:私はそれをどのように修正することができますか?

答えて

1
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     mLocationPermissionGranted = true; 
    } else { 
     ActivityCompat.requestPermissions(this, 
       new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
       PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 
    } 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             @NonNull String permissions[], 
             @NonNull int[] grantResults) { 
    mLocationPermissionGranted = false; 
    switch (requestCode) { 
     case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       mLocationPermissionGranted = true; 
       updateLocationUI(); 
      }else{ 
     mLocationPermissionGranted = true; 
     mMap.setMyLocationEnabled(false); 
     mMap.getUiSettings().setMyLocationButtonEnabled(false); 
     mLastKnownLocation = null; 
      } 
     } 
     break; 
    } 
} 
+0

あなたの答えの説明はありますか? –

+0

'requestpermission'の' override'メソッドのコードを置き換えて –

+0

私にこれを知らせるかどうかを教えてください。 –

1

使用このコード:

@Override 
public void onRequestPermissionsResult(int requestCode, 
             @NonNull String permissions[], 
             @NonNull int[] grantResults) { 
    mLocationPermissionGranted = false; 
    switch (requestCode) { 
     case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       mLocationPermissionGranted = true; 
       updateLocationUI(); 
      } 
     } 
    } 

} 
0

私は同じ問題を持っていたonRequestPermissionsResultが、私は問題を追加する固定しまったことが判明コード化されたことを書き換え、数回後の道:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

の他に:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

(AndroidManifest.xml)

幸運を祈る!