java.lang.ArrayIndexOutOfBoundsException: length=27; 指数=-1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37956729/
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-03 02:58:07  来源:igfitidea点击:

java.lang.ArrayIndexOutOfBoundsException: length=27; index=-1

javaandroid

提问by Dusan Dimitrijevic

I'm getting this error when i'm implementing footer within recycler view.

当我在回收站视图中实现页脚时出现此错误。

This is how i have done it. I was using two types for showing different views in list, but something is not set well in method getItemCount()or maybe when i'm getting position of clicked item model in list.

我就是这样做的。我使用两种类型在列表中显示不同的视图,但是方法中的某些内容设置得不好,getItemCount()或者当我在列表中获取单击的项目模型的位置时。

This is what i have so far:

这是我到目前为止:

private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_FOOTER = 1;

@Override
public int getItemCount() {
    return mUsers == null ? 0 : mUsers.size() + 1;
}

@Override
public int getItemViewType(int position) {
    if (isFooterPosition(position)) {
        return VIEW_TYPE_FOOTER;
    }
    return VIEW_TYPE_ITEM;
}

private boolean isFooterPosition(int position) {
    return position == mUsers.size() + 1;
}

private User getUser (int position) {
    return mUsers.get(position - 1); // Here i'm getting an error mentioned in title
}

Edit:

编辑:

if (holder instanceof UserHolder) {
        final User user = getUser(position);
        UserHolder userViewHolder = (UserHolder) holder;

        userViewHolder.tvUserName.setText(user.getName());
        userViewHolder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, position);
            }
        });
    } else if (holder instanceof FooterViewHolder) {
        FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
        Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "Lato-Thin.ttf");
        footerViewHolder.mButton.setTypeface(typeface);
    }

I have some items for normal holder view in list and one item for footer view.

我在列表中有一些用于普通持有人视图的项目和一个用于页脚视图的项目。

回答by Nick Felker

mUsers.get(position - 1);will crash when the positionis 0 because you're looking for the item at index -1which is invalid.

mUsers.get(position - 1);position为 0时会崩溃,因为您正在寻找-1无效的索引项。

If you're adding a footer, which will be present after all of the previous items, then do you need do the substraction?

如果您要添加页脚,它将出现在所有先前项目之后,那么您是否需要进行减法?

  • Position 0 => User 0
  • Position 1 => User 1
  • Position N => User N
  • Position N + 1 => Footer
  • 位置 0 => 用户 0
  • 位置 1 => 用户 1
  • 位置 N => 用户 N
  • 位置 N + 1 => 页脚

It might be better to just return mUsers.get(position).

直接返回可能会更好mUsers.get(position)

Edit: There's another small issue:

编辑:还有一个小问题:

Here's an issue:

这里有一个问题:

private boolean isFooterPosition(int position) {
    return position == mUsers.size() + 1;
}

mUsers.size()is 20, so users will have positions 0-19. isFooterPositionshould return true for 20 (users size + 1). However, that will return falsebecause the footer is at position 21.

mUsers.size()是 20,所以用户的位置是 0-19。 isFooterPosition应该为 20(用户大小 + 1)返回 true。但是,这将返回,false因为页脚位于位置21

Thus, you have a spot (20) that is completely invalid.

因此,您有一个20完全无效的点 ( )。

private boolean isFooterPosition(int position) {
    return position == mUsers.size();
}

回答by Alex Hong

I think you can modify getUser()method like following:

我认为你可以修改getUser()方法如下:

private boolean isValidPos(int position){
    return position >= 0 && position < mUsers.size();
}

private User getUser (int position) {
    if (isFooterPosition(position)) return null;

    return isValidPos(position) ? mUsers.get(position) : null; 
}

回答by Pedro Alves

I can't see exactly what you want to do, but, as someone said before me, the error is in mUsers.get(position - 1);, because it's gonna search for a negative index if position == 0.

我无法确切地看到您想要做什么,但是,正如之前有人所说,错误在 中mUsers.get(position - 1);,因为如果 ,它将搜索负索引position == 0

So, if you really need the subtraction, you can do like this:

所以,如果你真的需要减法,你可以这样做:

private User getUser (int position) {
    if(position != 0)
        return mUsers.get(position - 1);
    else
        return mUsers.get(position);
}

But, as you can see, for position == 0, it will return the same outputas position == 1.

但是,正如你所看到的,对position == 0,它会返回输出相同position == 1

回答by Dusan Dimitrijevic

I have found a solution and i have also extended RecyclerViewwith one more typeviewfor showing one view where there is no item:

我找到了一个解决方案,我还扩展RecyclerView了一个解决方案,typeview用于显示一个没有项目的视图:

First i have declared these variables in my adapter:

首先,我在适配器中声明了这些变量:

public static final int COUNT_FOOTER   = 1;
public static final int COUNT_NO_ITEMS = 1;

private final int VIEW_TYPE_ITEM    = 0;
private final int VIEW_TYPE_FOOTER  = 1;
private final int VIEW_TYPE_NO_ITEM = 2;

After that i have define three view types for my list:

之后,我为我的列表定义了三种视图类型:

@Override
    public int getItemViewType(int position) {
        if (!mUsers.isEmpty()) {
            if (position < mUsers.size()) {
                return VIEW_TYPE_ITEM;
            } else {
                return VIEW_TYPE_FOOTER;
            }
        } else {
            if (position == 0) {
                return VIEW_TYPE_NO_ITEM;
            } else {
                return VIEW_TYPE_FOOTER;
            }
        }
    }

@Override
public int getItemCount() {
    if (!mUsers.isEmpty()) {
        return mUsers.size() + COUNT_FOOTER;
    } else {
        return COUNT_NO_ITEMS + COUNT_FOOTER;
    }
}

private User getUser (int position) {
    return mUsers.get(position);
}