GVKun编程网logo

在索引处添加到ArrayList时出现IndexOutOfBoundsException(arraylist 索引)

17

在本文中,我们将详细介绍在索引处添加到ArrayList时出现IndexOutOfBoundsException的各个方面,并为您提供关于arraylist索引的相关解答,同时,我们也将为您带来关于A

在本文中,我们将详细介绍在索引处添加到ArrayList时出现IndexOutOfBoundsException的各个方面,并为您提供关于arraylist 索引的相关解答,同时,我们也将为您带来关于Android ArrayList的IndexOutOfBoundsException、android – TextView样式ArrayIndexOutOfBoundsException、ArrayIndexOutOfBoundsException 160、ArrayIndexOutOfBoundsException >> arr lenght 引起的异常的有用知识。

本文目录一览:

在索引处添加到ArrayList时出现IndexOutOfBoundsException(arraylist 索引)

在索引处添加到ArrayList时出现IndexOutOfBoundsException(arraylist 索引)

我得到Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1,Size: 0以下代码的异常。但是不明白为什么。

public class App {    public static void main(String[] args) {        ArrayList<String> s = new ArrayList<>();        //Set index deliberately as 1 (not zero)        s.add(1,"Elephant");        System.out.println(s.size());                    }}

更新资料

我可以使它起作用,但是我试图理解这些概念,因此我将声明更改为以下内容,但也没有起作用。

ArrayList<String> s = new ArrayList<>(10)

答案1

小编典典

ArrayList索引从0开始(零)

您的数组列表大小为0,并且要在第一个索引处添加String元素。如果不在第0个索引处添加元素,则无法添加下一个索引位置。错了

因此,只需使其成为

 s.add("Elephant");

或者你可以

s.add(0,"Elephant");

Android ArrayList的IndexOutOfBoundsException

Android ArrayList的IndexOutOfBoundsException

我有一些代码引发IndexOutOfBoundsException异常烦人的问题,我真的不明白为什么。logcat指向以下代码的“
addTimetableItem”,这将无法进一步说明:

if(sortedFridayTimes.size()>0){
    insertDay("Friday");
    for(int i=1; i<sortedFridayTimes.size()+1;i++){
        addTimetableItem(sortedFridayTimes.get(i));
    }
}


sortedFridayTimes”是一个包含我自己的“时间表条目”对象的ArrayList,我已经对其进行了排序。首先检查大小,以查看是否有任何对象,然后检查是否存在“
insertDay”,这将为标题创建一个新的textview并将其添加到布局中(这很好..)。在for循环内,想法是将arraylist中的所有对象添加到布局中。现在我知道“
addTimetableItem”代码已经通过ive测试,但是我的问题是我似乎无法从arraylist中获取最后一个对象。如果我声明for循环仅针对

"i<sortedFridayTimes.size()"

然后程序可以正常运行,但是我没有发现arraylist中存在的最后一个条目,因为我已经调试并监视了变量。如上所示,在添加“
+1”后,我现在得到了IndexOutOfBoundsException,但我真的不知道为什么。就像我说的那样,我已经调试了,而且我知道要尝试指向的arraylist中存在一个条目,但是它崩溃了。如果需要,我可以提供更多代码,但是有人有什么想法吗?

android – TextView样式ArrayIndexOutOfBoundsException

android – TextView样式ArrayIndexOutOfBoundsException

我在PlayStore的管理控制台中经常出现崩溃.我不明白为什么它会崩溃.
我从来没有重现过这次崩溃,它似乎只是来自三星galaxy设备(虽然不太确定).来自sdk 4.1版& 4.2& 4.3

这是完整的StackTrace:

android.view.InflateException: Binary XML file line #31: Error inflating class <unkNown>
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.android.internal.widget.ActionBarContextView.initClose(ActionBarContextView.java:262)
at com.android.internal.widget.ActionBarContextView.onConfigurationChanged(ActionBarContextView.java:136)
at android.view.View.dispatchConfigurationChanged(View.java:7761)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1056)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1060)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1060)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1060)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1060)
at android.view.ViewRootImpl.updateConfiguration(ViewRootImpl.java:2800)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
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:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
... 31 more
Caused by: java.lang.Arrayindexoutofboundsexception: length=24; index=691
at android.content.res.StringBlock.get(StringBlock.java:64)
at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:720)
at android.content.res.TypedArray.getString(TypedArray.java:124)
at android.widget.TextView.<init>(TextView.java:916)
at android.widget.TextView.<init>(TextView.java:562)
... 34 more

我想也许这是由我给TextView的风格(特别是fontFamily)引起的.所以这里是我的价值观风格-v16> styles.xml(values-v16):

<style name="TextViewHour">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">5dip</item>
        <item name="android:ellipsize">end</item>
        <item name="android:maxLines">1</item>
        <item name="android:textSize">@dimen/text_hour_size</item>
        <item name="android:textColor">@color/item_hour</item>
        <item name="android:fontFamily">sans-serif-condensed</item>
        <item name="android:textStyle">bold</item>
    </style>

来自价值观> styles.xml

<style name="TextViewHour">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">5dip</item>
        <item name="android:ellipsize">end</item>
        <item name="android:maxLines">1</item>
        <item name="android:textSize">@dimen/text_hour_size</item>
        <item name="android:textColor">@color/item_hour</item>
        <item name="android:textStyle">bold</item>
    </style>

这里是一个使用TextView的例子:

<TextViewandroid:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/item_date_separator"/>

我对我的应用程序在某些设备上崩溃的原因一无所知.
所以,如果有人有想法,我全都耳朵!

编辑:似乎它不仅在galaxy设备上,我也有这个Nexus 7的崩溃.

解决方法

字体sans-serif-condensed可能在某些设备上不可用,这就是应用程序崩溃的原因.如果要支持其他字体,可以将其放在assests文件夹中,然后通过扩展TextView创建CustomTextView

public class MyTextView extends TextView {

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

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

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

private void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"your_font.ttf");
    setTypeface(tf);
}

}

并在你的xml中.

<com.mypackage.test.MyTextView
 android:id="@+id/txt"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:layout_weight="1"
 android:text="custom text view. "
 android:textSize="30dip"
 android:textColor="#ff0000"
>

ArrayIndexOutOfBoundsException 160

ArrayIndexOutOfBoundsException 160

解决方法,升级fastjson的版本

ArrayIndexOutOfBoundsException >> arr lenght 引起的异常

ArrayIndexOutOfBoundsException >> arr lenght 引起的异常

如何解决ArrayIndexOutOfBoundsException >> arr lenght 引起的异常?

对 Arrayindexoutofboundsexception 有点熟悉但无法解决。

突出显示错误所在的行。

如果为数组的长度引入 5,程序将指出为什么 d 可能等于 arr 的长度值(在本例中为 5)。

我知道这是一个愚蠢的错误,因此我很生气......

已突出显示错误的行。

任何帮助??

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

关于在索引处添加到ArrayList时出现IndexOutOfBoundsExceptionarraylist 索引的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android ArrayList的IndexOutOfBoundsException、android – TextView样式ArrayIndexOutOfBoundsException、ArrayIndexOutOfBoundsException 160、ArrayIndexOutOfBoundsException >> arr lenght 引起的异常等相关内容,可以在本站寻找。

本文标签: