我在Play商店控制台中收到与android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback
相关的奇怪的越界异常java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.example.MyFragmentActivity.onRequestPermissionsResult(MyFragmentActivity.java:2068)
at android.app.Activity.requestPermissions(Activity.java:4163)
at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompat23.java:32)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:316)
at com.example.MyFragmentActivity.onConnected(MyFragmentActivity.java:2048)
at com.google.android.gms.common.internal.zzk.zzk(Unknown Source)
at com.google.android.gms.common.api.internal.zzj.zzi(Unknown Source)
at com.google.android.gms.common.api.internal.zzh.zzpx(Unknown Source)
at com.google.android.gms.common.api.internal.zzh.onConnected(Unknown Source)
at com.google.android.gms.common.api.internal.zzl.onConnected(Unknown Source)
at com.google.android.gms.common.api.internal.zzc.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzj$zzg.zzqL(Unknown Source)
at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
at com.google.android.gms.common.internal.zzj$zza.zzw(Unknown Source)
at com.google.android.gms.common.internal.zzj$zzc.zzqN(Unknown Source)
at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
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)
这是我的onRequestPermissionsResult实现
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_LOCATION_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
startLocationUpdates();
} else {
// Permission Denied
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
2068行是这样的:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
我相信它声称grantResults
长度为零,这就是抛出java.lang.ArrayIndexOutOfBoundsException的原因。根据{{3}}
int:授予相应权限的结果 PERMISSION_GRANTED或PERMISSION_DENIED。永远不会为空。
grantResults
不会为空,但也不会说任何关于它的内容也不包含值。此外,回调似乎至少包含一个值,PERMISSION_GRANTED
或PERMISSION_DENIED
。这是一个错误还是我误解了文档?
答案 0 :(得分:45)
Documentation about permissions说
如果取消请求,结果数组为空。
似乎是你的情况,所以你必须首先检查数组是否为空:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_LOCATION_PERMISSIONS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
startLocationUpdates();
} else {
// Permission Denied
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
答案 1 :(得分:0)
科特林代码检查权限并修复此错误: 最好在Kotlin中使用.isNotEmpty()检查IntArray的大小。
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://www.ratestar.in/')
inputElement = driver.find_element_by_css_selector("#txtStock")
inputElement.send_keys('GM Breweries')
inputElement.send_keys(Keys.ENTER)
driver.wait.until(staleness_of('txtStock')
source = driver.page_source
soup = BeautifulSoup(source)