我从https://github.com/googlemaps/android-samples/tree/master/tutorials/CurrentPlaceDetailsOnMap下载了示例源代码。
它在第一次运行时因两个函数之间的无限循环而崩溃:
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)
问题:我该如何解决?
答案 0 :(得分:2)
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;
}
}
答案 1 :(得分: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();
}
}
}
}
答案 2 :(得分:0)
我遇到了同样的问题,并且在重写了onRequestPermissionsResult编码的方式几次之后,我发现问题得到了修复补充:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
除了:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
在AndroidManifest.xml
祝你好运!