Android java.lang.ArrayIndexOutOfBoundsException: length=10; 指数=10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17047172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:48:22  来源:igfitidea点击:

Android java.lang.ArrayIndexOutOfBoundsException: length=10; index=10

javaandroid

提问by Paul

I'm having trouble figuring out this one out. I'm unable to reproduce on my phone (nexus 4). Doesn't seem to effect all devices but I have enough reports where it's frustrating to see it keep occurring.

我很难弄清楚这一点。我无法在我的手机上重现(nexus 4)。似乎不会影响所有设备,但我有足够的报告,看到它不断发生令人沮丧。

java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
at android.text.StaticLayout.calculateEllipsis(StaticLayout.java:938)
at android.text.StaticLayout.out(StaticLayout.java:859)
at android.text.StaticLayout.generate(StaticLayout.java:524)
at android.text.StaticLayout.<init>(StaticLayout.java:147)
at android.widget.TextView.makeSingleLayout(TextView.java:6583)
at android.widget.TextView.makeNewLayout(TextView.java:6429)
at android.widget.TextView.onMeasure(TextView.java:6807)
at android.view.View.measure(View.java:16047)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1232)
at android.widget.TableRow.onMeasure(TableRow.java:114)
at android.view.View.measure(View.java:16047)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4921)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.TableLayout.measureVertical(TableLayout.java:476)
at android.widget.TableLayout.onMeasure(TableLayout.java:439)
at android.view.View.measure(View.java:16047)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4921)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16047)
at android.widget.ListView.measureScrapChild(ListView.java:1190)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1255)
at android.widget.ListView.onMeasure(ListView.java:1165)
at android.view.View.measure(View.java:16047)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4921)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16047)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4921)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16047)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16047)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4921)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2418)
at android.view.View.measure(View.java:16047)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2129)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1282)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1493)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1179)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4861)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

回答by user2738318

Need to set attributes android:lines="1" android:singleLine="true"https://code.google.com/p/android/issues/detail?id=33868

需要设置属性android:lines="1" android:singleLine="true"https://code.google.com/p/android/issues/detail?id=33868

回答by Rakesh Sen

This is a bug. reported in android itself please ignore to use start. https://code.google.com/p/android/issues/detail?id=33868

这是一个错误。android本身报请忽略使用start。 https://code.google.com/p/android/issues/detail?id=33868

回答by Vasanth

Issue

问题

  1. If we set android:maxLines="1" android:ellipsize="middle"then our application crashes in some devices.
  1. 如果我们设置,android:maxLines="1" android:ellipsize="middle"那么我们的应用程序会在某些设备上崩溃。

Solution

解决方案

  1. Android documentation says that when we set "android:maxLines"property then for "android:ellipsize"property only Endand Marqueeis supported.
  1. Android 文档说,当我们设置"android:maxLines"属性时,"android:ellipsize"仅支持EndMarquee属性。

Reference

参考

setEllipsize

设置椭圆大小

added in API level 1

在 API 级别 1 中添加

void setEllipsize (TextUtils.TruncateAt where)

Causes words in the text that are longer than the view's width to be ellipsized instead of broken in the middle. You may also want to setSingleLine() or setHorizontallyScrolling(boolean) to constrain the text to a single line. Use null to turn off ellipsizing. If setMaxLines(int) has been used to set two or more lines, only END and MARQUEE are supported (other ellipsizing types will not do anything).

导致文本中长于视图宽度的单词被椭圆化而不是在中间断开。您可能还需要 setSingleLine() 或 setHorizo​​ntallyScrolling(boolean) 将文本限制为单行。使用 null 关闭省略号。如果 setMaxLines(int) 已经用于设置两行或更多行,则仅支持 END 和 MARQUEE(其他省略号类型不会做任何事情)。

回答by Frank

They should have at least added this to Lint! :S You can use a custom textview to fix this.

他们至少应该将这个添加到 Lint 中!:S 您可以使用自定义文本视图来解决此问题。

public class MyTextView extends TextView {
    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

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

    public MyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {

        // this is to overcome the calculateEllipsis bug in some versions of android, I spotted it on 4.4.4 and 4.4.3
        // see https://code.google.com/p/android/issues/detail?id=33868

        if (Build.VERSION.SDK_INT >= 16) {
            if (getMaxLines() == 1) {
                setSingleLine(true);
            }
        }
    }
}

reference MyTextView in your xml instead of the normal textview.

在您的 xml 中引用 MyTextView 而不是普通的 textview。

回答by Suresh Atta

Arrayindices begin at 0and end at the array lengthminus 1..So limit loop to 9.

Array索引以减号开始0并以array length减号结束。1所以将循环限制为9.

回答by MalhotraUrmil

change the for loop look like this

改变 for 循环看起来像这样

your array length 0 to 10

你的数组长度 0 到 10

for (int i=0; i<9; i++)
{
      System.out.println("value is  ofposition"+yourArrayList[i]);
}