这是我遇到的异常:
java.lang.SecurityException
:com.google.android.gms.DynamiteModulesB line 50297 in az.c()
my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
az : android.support.transition.TransitionIcs
c() : android.support.transition.TransitionIcs$CompatListener mCompatListener
com.google.maps.api.android.lib6.impl
:com.google.android.gms.DynamiteModulesB line 50297 in az.c()
com.google.android.gms.maps.internal
:com.google.android.gms.DynamiteModulesB line 274 in l.onTransact()
android.os
Binder.java line 387 in Binder.transact()
com.google.android.gms.maps.internal
line -1 IGoogleMapDelegate$zza$a.setMyLocationEnabled()
com.google.android.gms.maps
line -1 GoogleMap.setMyLocationEnabled()
SourceFile line 214 in MapsActivity.onMapReady()
com.google.android.gms.maps
line -1 MapFragment$a$1.zza()
com.google.android.gms.maps.internal
line -1 zzo$zza.onTransact()
android.os
Binder.java line 387 in Binder.transact()
com.google.android.gms.maps.internal
:com.google.android.gms.DynamiteModulesB line 82 in bw.a()
com.google.maps.api.android.lib6.impl
:com.google.android.gms.DynamiteModulesB line 1805 in bf.run()
android.os
Handler.java line 739 in Handler.handleCallback()
android.os
Handler.java line 95 in Handler.dispatchMessage()
android.os
Looper.java line 234 in Looper.loop()
android.app
ActivityThread.java line 5526 in ActivityThread.main()
java.lang.reflect
Method.java line -2 in Method.invoke()
com.android.internal.os
ZygoteInit.java line 726 in ZygoteInit$Method
我有一个使用 getMap 方法的 map fragment ,如果设备中启用了位置,则显示用户的当前位置,但有时它会给我上述异常,我在代码中添加了一些位置权限检查,但我不这样做不知道我错过了什么...
感谢帮助
这里有一些附加数据:
compileSdkVersion 25
minSdkVersion 15
targetSdkVersion 25
@Override
public void onMapReady(final GoogleMap map) {
map.setMyLocationEnabled(true);
}
最佳答案
将这些添加到 AndroidManifest.xml
中 application
标记之前
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
同样对于Android版本> = 23,您需要在运行时请求位置权限
if (ContextCompat.checkSelfPermission(YourActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&&
ContextCompat.checkSelfPermission(YourActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
askForLocationPermissions();
} else {
//do your work
}
还有
private void askForLocationPermissions() {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("Location permessions needed")
.setMessage("you need to allow this permission!")
.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(YourActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// //Do nothing
}
})
.show();
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
还有
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
//Do you work
} else {
Toast.makeText(this, "Can not proceed! i need permission" , Toast.LENGTH_SHORT).show();
}
break;
}
}
还有
public static boolean isPermissionGranted(@NonNull String[] grantPermissions, @NonNull int[] grantResults,
@NonNull String permission) {
for (int i = 0; i < grantPermissions.length; i++) {
if (permission.equals(grantPermissions[i])) {
return grantResults[i] == PackageManager.PERMISSION_GRANTED;
}
}
return false;
}
关于Android Google Maps API API23 上的安全异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016164/