数组索引在Android中超出范围

时间:2018-06-12 11:30:28

标签: android

我在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)"

我使用以下类来设置文本字体。请看下面的课程。

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;
    }
}

4 个答案:

答案 0 :(得分:0)

根据日志:

length=3; index=3

数组长度为3 ,您正在尝试获取3的索引。检查您的计数器是否从0开始或退出条件是否合适。

答案 1 :(得分:0)

致命异常:java.lang.ArrayIndexOutOfBoundsException 长度= 3;索引= 3

你得到的这个例外是因为你的数组列表或数组只有3个项目但你想要访问索引3的项目意味着项目列表中不存在的项目,

因此,在访问特定项目之前,请提出一个条件

就像索引不应该大于列表大小

if(index<arraylist.zise()){
   Then accesss here your item From list
}

答案 2 :(得分:0)

更新:

测试你的&#39; a&#39; array lenght使用之前。

new TypedArray(buffer [, byteOffset [, length]]);

double [] myList = new double [10];

检查我的列表是否有10个位置,但在图片中我们可以访问myList[9]因为索引从myList[0]开始

enter image description here

答案 3 :(得分:0)

使用类似的东西

String price = "1,000,000,000.999999999999999";
BigDecimal result = new BigDecimal(value.replaceAll(",", ""));
System.out.println(result);