android – ViewHolder ArrayIndexOutOfBoundsException:length = 2;索引= 2

android – ViewHolder ArrayIndexOutOfBoundsException:length = 2;索引= 2,第1张

概述我尝试使用两种类型的视图和两种类型的ViewHolrdes,但收到错误: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6705) at android 我尝试使用两种类型的视图和两种类型的VIEwHolrdes,但收到错误:
java.lang.Arrayindexoutofboundsexception: length=2; index=2        at androID.Widget.AbsListVIEw$RecycleBin.addScrapVIEw(AbsListVIEw.java:6705)        at androID.Widget.AbsListVIEw.trackMotionScroll(AbsListVIEw.java:5210)        at androID.Widget.AbsListVIEw$FlingRunnable.run(AbsListVIEw.java:4368)        at androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:776)        at androID.vIEw.Choreographer.doCallbacks(Choreographer.java:579)        at androID.vIEw.Choreographer.doFrame(Choreographer.java:547)        at androID.vIEw.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:762)        at androID.os.Handler.handleCallback(Handler.java:800)        at androID.os.Handler.dispatchMessage(Handler.java:100)        at androID.os.Looper.loop(Looper.java:194)        at androID.app.ActivityThread.main(ActivityThread.java:5371)        at java.lang.reflect.Method.invokeNative(Native Method)        at java.lang.reflect.Method.invoke(Method.java:525)        at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)        at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:600)        at dalvik.system.NativeStart.main(Native Method)

这是我的适配器:

public class FrIEndslistadapterFromKesh extends ArrayAdapter<FrIEndListEntryItem> {    List<FrIEndListEntryItem> frIEnds;    List<FrIEndListEntryItem> frIEndsWithoutPoints;    Context context;    private LayoutInflater inflater;    private LayoutInflater mLayoutInflater;    private static String ROOT_DIRECTORY_PATH = Environment.getExternalStorageDirectory() + file.separator + ".SleepKeeker/Photos old";    FrIEndsTab frIEndsTab;    public FrIEndslistadapterFromKesh(Context context,final List<FrIEndListEntryItem> frIEnds,final List<FrIEndListEntryItem> frIEndsWithoutPoints) {        super(context,0);        this.context = context;        inflater = LayoutInflater.from(context);        this.frIEndsWithoutPoints = frIEndsWithoutPoints;        this.frIEnds = frIEnds;        frIEndsTab = new FrIEndsTab();        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    }    public voID updateList(List<FrIEndListEntryItem> newList1,List<FrIEndListEntryItem> newList2) {        frIEnds.clear();        frIEnds.addAll(newList1);        frIEndsWithoutPoints.clear();        frIEndsWithoutPoints.addAll(newList2);        this.notifyDataSetChanged();    }    public List<FrIEndListEntryItem> getFrIEnds() {        return frIEnds;    }    @OverrIDe    public int getCount() {        return frIEnds.size() + frIEndsWithoutPoints.size();    }    public String getIDSocTypename(int position) {        if (frIEnds == null || position + 1 >= frIEnds.size()) {            return "";        }        FrIEndListEntryItem ei = frIEnds.get(position - 1);        return ei.userID + "," + ei.socType + "," + ei.name;    }    static class VIEwHolder1 {        public ImageVIEw image = null;        public TextVIEw Title = null;        public TextVIEw subTitle = null;    }    static class VIEwHolder2 {        public ImageVIEw image;        public TextVIEw Title;        public Typefacedbutton button;    }    @OverrIDe    public int getItemVIEwType(int pos) {        if (pos < frIEnds.size())            return 1;        return 2;    }    //@TargetAPI(Build.VERSION_CODES.JELLY_BEAN)    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {        int vIEwType = getItemVIEwType(position);        VIEwHolder1 vIEwHolder1 = null;        VIEwHolder2 vIEwHolder2 = null;        if (convertVIEw == null) {            if (vIEwType == 1) {                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                convertVIEw = inflater.inflate(R.layout.List_frnds_item_with_points,parent,false);                vIEwHolder1 = new VIEwHolder1();                vIEwHolder1.Title = (TextVIEw) convertVIEw.findVIEwByID(R.ID.List_item_Title_frIEnd);                vIEwHolder1.image = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imageFrIEndAva);                vIEwHolder1.subTitle = (TextVIEw) convertVIEw.findVIEwByID(R.ID.List_item_frIEnd_woke_time);                convertVIEw.setTag(vIEwHolder1);            } else {                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                convertVIEw = inflater.inflate(R.layout.List_frnds_item_without_points,false);                vIEwHolder2 = new VIEwHolder2();                vIEwHolder2.Title = (TextVIEw) convertVIEw.findVIEwByID(R.ID.List_item_Title_frIEnd);                vIEwHolder2.image = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imageFrIEndAva);                vIEwHolder2.button = (Typefacedbutton) convertVIEw.findVIEwByID(R.ID.invitebutton);                vIEwHolder2.button.setClickListner();                convertVIEw.setTag(vIEwHolder2);            }        } else {            if (vIEwType == 1) {                vIEwHolder1 = (VIEwHolder1) convertVIEw.getTag();            } else {                vIEwHolder2 = (VIEwHolder2) convertVIEw.getTag();            }        }        if (vIEwType == 1) {             final FrIEndListEntryItem ei = frIEnds.get(position);             vIEwHolder1.Title.setText(ei.name);             vIEwHolder1.subTitle.setText(ei.wokeTime);             file picture = findPicture(ei.name);             if (picture != null && picture.exists()) {                    Picasso.with(context).load("file://" + picture.getabsolutePath()).fit().centerCrop().into(vIEwHolder1.image);             }        } else {                final FrIEndListEntryItem ei2 = frIEndsWithoutPoints.get(position - frIEnds.size());                vIEwHolder2.Title.setText(ei2.name);                vIEwHolder2.button.setFrIEndItem(ei2);                file picture = findPicture(ei2.name);                if (picture != null && picture.exists()) {                    Picasso.with(context).load("file://" + picture.getabsolutePath()).fit().centerCrop().into(vIEwHolder2.image);                }         }      return convertVIEw;    }    @OverrIDe    public int getVIEwTypeCount() {        return 2;    }    private file findPicture(String name) {        file root = new file(ROOT_DIRECTORY_PATH);        if (root!= null)        {            file[] files = root.Listfiles();            if (files == null)                return null;            for (int i = 0; i<files.length; i++) {                Log.d("qedsds","" + files[i].getname());                if (files[i].getname().contains(name))                    return files[i];            }        }        return null;    }}

滚动列表时出现错误.但我不明白为什么.
我花了很多时间来解决这个问题,但无济于事

解决方法 更换
@OverrIDepublic int getItemVIEwType(int pos) {    if (pos < frIEnds.size())        return 1;    return 2;}

通过

@OverrIDepublic int getItemVIEwType(int pos) {    if (pos < frIEnds.size())        return 0;    return 1;}

从the Adapter documentation for the method getItemViewType(我的重点):

Returns

An integer representing the type of VIEw. Two vIEws should share the same type if one can be converted to the other in getVIEw(int,VIEw,VIEwGroup). Note: Integers must be in the range 0 to getVIEwTypeCount() – 1. IGnorE_ITEM_VIEW_TYPE can also be returned.

此时,您将返回视图类型的值1和2.您需要返回值0和1,以满足粗体条件.

总结

以上是内存溢出为你收集整理的android – ViewHolder ArrayIndexOutOfBoundsException:length = 2;索引= 2全部内容,希望文章能够帮你解决android – ViewHolder ArrayIndexOutOfBoundsException:length = 2;索引= 2所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存