Android ArrayIndexOutOfBoundsException和AbsListViewRecycleBin.addScrapView

时间:2014-09-09 16:16:19

标签: java android arraylist indexoutofboundsexception

我随机出现ArrayIndexOutOfBoundsException。它似乎发生在我的notifyDataSetChanged();期间。由于错误是如此随机,因此很难准确指出它发生的位置。

是否有人在自定义Adaptor上遇到类似问题?

FATAL EXCEPTION: main
 java.lang.ArrayIndexOutOfBoundsException: length=12; index=12
    at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:8041)
    at android.widget.ListView.layoutChildren(ListView.java:1604)
    at android.widget.AbsListView.onLayout(AbsListView.java:2444)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:15221)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2260)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2007)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
    at android.view.Choreographer.doFrame(Choreographer.java:561)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5455)
    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:1187)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    at dalvik.system.NativeStart.main(Native Method)




public class CarUpfitScanvinadapter extends BaseAdapter{
    @SuppressWarnings("unused")
    private final String TAG = this.getClass().getSimpleName();
    private Activity mActivity;
    private ArrayList<CarUpfitModel> mData;
    private static LayoutInflater sInflater = null;
    public int height = 0;
    public int heightSet = 0;
    private CarUpfitModelForm mForm;
    private ScanlistListener mCallback;

    public CarUpfitScanvinadapter(Activity a, ArrayList<CarUpfitModel> d, Resources resLocal){
        mActivity = a;
        mData = d;
        sInflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void removeItem(int i){
        mData.remove(i);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        Log.v(TAG, "getCount");
        if(mData.size()<=0) return 1;
        return mData.size();
    }

    @Override
    public boolean hasStableIds (){
        return false;
    }

    @Override
    public int getViewTypeCount() {                 
        return getCount();
    }

    @Override
    public CarUpfitModel getItem(int position) {
        return mData.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(convertView == null){
            vi = sInflater.inflate(R.layout.subaruupfitscanlistview, parent, false);
            mForm = new CarUpfitModelForm();
            mForm.setllScanlist((LinearLayout) vi.findViewById(R.id.llScanlist));
            mForm.getllScanlist().setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,heightSet));
            mForm.setllheightset((LinearLayout) vi.findViewById(R.id.llheightset));
            mForm.setetModel((EditText) vi.findViewById(R.id.etModel));
            mForm.settvScanlistvin((TextView) vi.findViewById(R.id.tvScanlistvin));
            mForm.settvScanlistdate((TextView) vi.findViewById(R.id.tvScanlistdate));
            mForm.setcbGbkit((CheckBox) vi.findViewById(R.id.cbGbkit));
            mForm.setcbFltmat((CheckBox) vi.findViewById(R.id.cbFltmat));
            mForm.setcbFlmatbrk((CheckBox) vi.findViewById(R.id.cbFlmatbrk));
            mForm.setcbEyesight((CheckBox) vi.findViewById(R.id.cbEyesight));
            mForm.setcbTrnkpan((CheckBox) vi.findViewById(R.id.cbTrnkpan));
            mForm.setcbIntmirror((CheckBox) vi.findViewById(R.id.cbIntmirror));
            mForm.setcbExtmirror((CheckBox) vi.findViewById(R.id.cbExtmirror));
            mForm.setcbPzevbadge((CheckBox) vi.findViewById(R.id.cbPzevbadge));
            mForm.setivDelete((ImageView) vi.findViewById(R.id.ivDelete));
            mForm.setivSave((ImageView) vi.findViewById(R.id.ivSave));
            mForm.getivSave().setTag(mForm);
            vi.setTag(mForm);
        }else{
            mForm = (CarUpfitModelForm) vi.getTag();
        }
        mForm.getllScanlist().setOnClickListener(new OnItemClickListener(position, CarUpfit.ITEM_CLICK));
        if(mData.size()<=0){
            mForm.puttvScanlistvin("No Data");
        }else{
            mForm.puttvScanlistvin(getItem(position).getVin());
            mForm.puttvScanlistdate(getItem(position).getDate());
            mForm.setId(getItem(position).getid());
            mForm.putjson(getItem(position).getData());
            mForm.getivSave().setOnClickListener(new OnItemClickListener(position, CarUpfit.ITEM_UPDATE));
            mForm.getivDelete().setOnClickListener(new OnItemClickListener(position, CarUpfit.ITEM_REMOVE));
        }
        return vi;
    }

    private class OnItemClickListener implements OnClickListener{
        private int mPosition;
        private int mType;

        OnItemClickListener(int position, int type){
            mPosition = position;
            mType = type;
        }
        @Override
        public void onClick(View v) {
            mCallback.scanlistclick(v, mPosition, mType);
        }
    }

    public void setimplements(CarUpfit sl) {
        try {
            mCallback = (ScanlistListener) sl;
        } catch (ClassCastException e) {
            throw new ClassCastException("CarUpfit must implement ScanlistListener");
        }
    }

    public interface ScanlistListener{
        public void scanlistclick(View v, int position, int type);
    }
}

EDITS

我有一个包含动画的项目列表,可以展开列表项目。这就是我在getViewTypeCount()上覆盖的原因。如果我在用户打开项目时不覆盖它,它也会打开其他项目。

已关闭项目的屏幕截图:

items closed

项目打开的屏幕截图:

enter image description here

3 个答案:

答案 0 :(得分:14)

如果所有列表项都是统一的,或者至少让它们分别返回getViewTypeCount()getItemViewType(),则不应覆盖10

通过以你的方式覆盖它们,首先你禁止ListView的视图回收机制,其次这可能导致你描述的崩溃,因为当你从列表中间删除一个项目时,删除后的视图类型会发生变化。

更新:实际上,这就是正在发生的事情:ListView仅在getViewTypeCount()中调用适配器上的setAdapter()并创建该大小的内部数组。因此,如果您稍后添加观看次数并且getItemViewType()返回的索引大于或等于该大小,则会获得ArrayIndexOutOfBoundsException

更新2 :如果您需要可扩展列表视图,只需使用ExpandableListView

更新3:好的,我会尽可能彻底地解释。

没有理由在屏幕上打开视图。 ListView旨在执行回收,并且在99.9%的时间内不需要它,并且您的情况不会留下0.01%。

您的适配器应该记住所有细节,并在滚动ListView时填充新出现的视图。

在您的情况下,您有两种类型的观看 - 开放和关闭,因此getViewTypeCount()应该返回2而getItemViewType()应该返回01,具体取决于视图关闭或开放。

getView()中,如果convertView为空,则应根据该位置的getItemViewType()返回值创建它。如果它不为null,则可以保证它是您之前为同一类型创建的视图之一。

如果你的getItemViewType()返回任意大数字(我指的是API 19的来源),就会出现崩溃的现象:

// ListView.java
public void setAdapter(ListAdapter adapter) {
    ...
    // This is the only call to getViewTypeCount()
    mRecycler.setViewTypeCount(mAdapter.getViewTypeCount());
    ...
}

// AbsListView.java
public void setViewTypeCount(int viewTypeCount) {
    ...
    // Here it creates the array of given size
    ArrayList<View>[] scrapViews = new ArrayList[viewTypeCount];
    ...
    mViewTypeCount = viewTypeCount;
    mScrapViews = scrapViews;
}

void addScrapView(View scrap, int position) {
    final int viewType = lp.viewType;
    ...
    // And here you get ArrayIndexOutOfBoundsException
    mScrapViews[viewType].add(scrap);
}

答案 1 :(得分:1)

当getItemViewType返回超过getViewTypeCount大小的整数时,也会发生这种情况,因此它们就像索引一样。例如。 getViewTypeCount 2要求getItemViewType为0和1。

答案 2 :(得分:0)

您认为索引超出范围会发生更改通知数据集。我同意。例如,当您删除项目时。在该项之后的所有on item click侦听器将具有错误的mPosition值。