gpt4 book ai didi

android - Android 中数组索引越界

转载 作者:行者123 更新时间:2023-12-02 19:24:50 26 4
gpt4 key购买 nike

我在 crashlytics 上遇到以下异常。请帮我解决这个问题

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException
length=3; index=3
android.util.ContainerHelpers.binarySearch (ContainerHelpers.java:47)
android.util.LongSparseArray.get (LongSparseArray.java:113)
android.util.LongSparseArray.get (LongSparseArray.java:104)
android.graphics.Typeface.create (Typeface.java:177)
arrow_right
android.support.v4.graphics.TypefaceCompatApi24Impl.createFromFontInfo (Unknown Source)
android.support.v4.graphics.TypefaceCompat.createFromFontInfo (Unknown Source)
android.support.v4.provider.FontsContractCompat.buildTypeface (Unknown Source)
android.support.v4.provider.FontsContractCompat$4.run (Unknown Source)
android.os.Handler.handleCallback (Handler.java:761)
android.os.Handler.dispatchMessage (Handler.java:98)
android.os.Looper.loop (Looper.java:156)
android.os.HandlerThread.run (HandlerThread.java:61)

我认为这个问题与android中的typeFace有关,如上面异常中所写

"android.support.v4.graphics.TypefaceCompatApi24Impl.createFromFontInfo (Unknown Source)"

我正在使用以下类来设置文本字体。请看下面的类(class)。

public class CustomTextView extends android.support.v7.widget.AppCompatTextView {

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}

public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);

}

public CustomTextView(Context context) {
super(context);
init(null);
}

public void init(AttributeSet attrs) {


try {
if (getContext()!=null &&attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_fontName);

if (fontName != null) {
requestDownload(fontName);
}
a.recycle();
}

this.setIncludeFontPadding(false);
} catch (Exception e) {
e.printStackTrace();
}
}

public void setTypeFaceBold() {
try {
requestDownload("poppinsbold.ttf");
}catch (Exception e){
e.printStackTrace();
}



}

public void setTypeFaceLight() {
try {
requestDownload("poppinslight.ttf");
}catch (Exception e){
e.printStackTrace();

}


}

public void setTypeFaceMedium() {
try {
requestDownload("poppinsmedium.ttf");
}catch (Exception e){
e.printStackTrace();
}

}

public void setTypeFaceRegular() {
try {
requestDownload("poppinsregular.ttf");
}catch (Exception e){
e.printStackTrace();
}

}

public void setTypeFaceSemiBold() {
try {
requestDownload("poppinssemibold.ttf");
}catch (Exception e){
e.printStackTrace();
}



}

private Handler mHandler = null;

private void requestDownload(final String familyName) {
try {
int widthValue = (int) (100 * (float) FontConstants.WIDTH_DEFAULT / (float) FontConstants.WIDTH_MAX);
float weightValue = (float) FontConstants.WEIGHT_DEFAULT / (float) FontConstants.WEIGHT_MAX * 100;
float italicValue = (float) FontConstants.ITALIC_DEFAULT;

if (familyName.equals("poppinslight.ttf")) {
weightValue = 300;
if(BaseActivity.poppins_light!=null){
this.setTypeface(BaseActivity.poppins_light);
return;
}
} else if (familyName.equals("poppinsmedium.ttf")) {
weightValue = 500;
if(BaseActivity.poppins_medium!=null){
this.setTypeface(BaseActivity.poppins_medium);
return;
}
} else if (familyName.equals("poppinsregular.ttf")) {
weightValue = 400;
if(BaseActivity.poppins!=null){
this.setTypeface(BaseActivity.poppins);
return;
}
} else if (familyName.equals("poppinssemibold.ttf")) {
weightValue = 600;
if(BaseActivity.poppins_semibold!=null){
this.setTypeface(BaseActivity.poppins_semibold);
return;
}
} else if (familyName.equals("poppinsbold.ttf")) {
weightValue = 700;
if(BaseActivity.poppins_bold!=null){
this.setTypeface(BaseActivity.poppins_bold);
return;
}
}

QueryBuilder queryBuilder = new QueryBuilder("Poppins")
.withWidth(widthValue)
.withWeight((int) weightValue)
.withItalic(italicValue)
.withBestEffort(true);

String query = queryBuilder.build();

FontRequest request = new FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
query,
R.array.com_google_android_gms_fonts_certs);

FontsContractCompat.FontRequestCallback callback = new FontsContractCompat.FontRequestCallback() {
@Override
public void onTypefaceRetrieved(Typeface typeface) {
try {
if (familyName.equals("poppinslight.ttf")) {
BaseActivity.poppins_light = typeface;
} else if (familyName.equals("poppinsmedium.ttf")) {
BaseActivity.poppins_medium = typeface;
} else if (familyName.equals("poppinsregular.ttf")) {
BaseActivity.poppins = typeface;
} else if (familyName.equals("poppinssemibold.ttf")) {
BaseActivity.poppins_semibold = typeface;
} else if (familyName.equals("poppinsbold.ttf")) {
BaseActivity.poppins_bold = typeface;
}


CustomTextView.this.setTypeface(typeface);
} catch (Exception e){
e.printStackTrace();
}
}

@Override
public void onTypefaceRequestFailed(int reason) {
Log.d("font", "failed");
/**
* Failed
*/
}
};
FontsContractCompat
.requestFont(getContext(), request, callback,
getHandlerThreadHandler());
} catch (Exception e){
e.printStackTrace();
}
}

private Handler getHandlerThreadHandler() {
try{
if (mHandler == null) {
HandlerThread handlerThread = new HandlerThread("fonts");
handlerThread.start();
mHandler = new Handler(handlerThread.getLooper());
}
}catch (Exception e){
e.printStackTrace();
}
return mHandler;
}
}

最佳答案

如果您来到这里遇到相同的异常,但您的异常与任何库都没有任何关系,并且您会看到类似以下内容:

java.lang.ArrayIndexOutOfBoundsException: length=11; index=11
at android.util.ContainerHelpers.binarySearch(ContainerHelpers.java:28)
at android.util.SparseIntArray.get(SparseIntArray.java:108)

问题出在 SparseArray 内部。我发现的唯一解决方案是用 HashMap 替换 SparseArray 以避免 BinarySearch 操作。

这里是有用的 Github 的链接 issueHere你可以找到提交,它似乎解决了问题。

如果问题发生在某些库内部 - 尝试更新到最新版本或暂时降级版本并观察更新。

关于android - Android 中数组索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50816075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com