2 lijian0927 LIJIAN0927 于 2017.01.07 10:35 提问

android数组越界问题 怎么处理越界了

public class MyHorizontalScrollView extends HorizontalScrollView {
private int subChildCount = 0;
private ViewGroup firstChild = null;
private int downX = 0;
public int currentPage = 0;
int paddle;
private ArrayList pointList = new ArrayList();

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

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

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

private void init() {
    setHorizontalScrollBarEnabled(false);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    receiveChildInfo();
}

public void receiveChildInfo() {

    firstChild = (ViewGroup) getChildAt(0);
    if (firstChild != null) {
        subChildCount = firstChild.getChildCount();
        for (int i = 0; i < subChildCount; i++) {
            if (((View) firstChild.getChildAt(i)).getWidth() > 0) {
                Log.i("hx",
                        "MyHorizontalScrollView---->receiveChildInfo:"
                                + i
                                + "  "
                                + ((View) firstChild.getChildAt(i))
                                        .getLeft());
                pointList.add(((View) firstChild.getChildAt(i)).getLeft());
            }
        }
    }

}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        downX = (int) ev.getX();
        Log.i("hx", "onInterceptTouchEvent---->ACTION_DOWN::" + downX);
        break;
    }
    return super.onInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // Log.i("PLJ", "MyHorizontalScrollView---->ACTION_DOWN::"+downX);
        break;
    case MotionEvent.ACTION_MOVE: {

    }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        if (currentPage == 0) {
            paddle = getWidth() / 5;
        } else {
            paddle = getWidth() / 10;
        }
        if (Math.abs((ev.getX() - downX)) > paddle) {
            if (ev.getX() - downX > 0) {
                Log.i("hx", "MyHorizontalScrollView---->ACTION_UP:111:"
                        + (ev.getX() - downX) + "   " + ev.getX() + "   "
                        + downX + "  " + getWidth() + "  " + paddle);
                smoothScrollToPrePage((int) Math.abs((ev.getX() - downX)));
            } else {
                Log.i("hx", "MyHorizontalScrollView---->ACTION_UP:222:"
                        + (ev.getX() - downX) + "   " + ev.getX() + "   "
                        + downX + "  " + getWidth() + "  " + paddle);
                smoothScrollToNextPage((int) Math.abs((ev.getX() - downX)));
            }
        } else {
            Log.i("hx",
                    "MyHorizontalScrollView---->ACTION_UP:333:"
                            + (ev.getX() - downX) + "   " + ev.getX()
                            + "   " + downX + "  " + getWidth() + "  "
                            + paddle);
            smoothScrollToCurrent();
        }
        return true;
    }
    }
    return super.onTouchEvent(ev);
}

/** 当前页 **/
private void smoothScrollToCurrent() {
    smoothScrollTo(pointList.get(currentPage), 0);
}

/** 上一页 **/
private void smoothScrollToPrePage(int intPre) {
    Log.i("hx", "MyHorizontalScrollView---->smoothScrollToPrePage:111:"
            + (currentPage > 0) + "  " + currentPage);
    if (currentPage > 0) {
        currentPage -= Zoom(intPre);
        if (currentPage < 0) {
            currentPage = 0;
        }
        /* currentPage--; */Log.i("hx",
                "MyHorizontalScrollView---->smoothScrollToPrePage:222:"
                        + currentPage + "   " + pointList.get(currentPage));
        smoothScrollTo(pointList.get(currentPage), 0);
    }
}

// private int PreZoom(int intPre){
// int page = 0;
// for(int i=0;i<pointList.size();i++){
// if((pointList.get(i+1) > intPre) && (intPre > pointList.get(i))){
// //if(i != 0){
// page = i+1;
// //}
// break;
// }
// } Log.i("PLJ", "MyHorizontalScrollView---->PreZoom:"+intPre+"  "+page);
// return page;
// }

/** 下一页 **/
private void smoothScrollToNextPage(int intNext) {
    Log.i("hx", "MyHorizontalScrollView---->smoothScrollToNextPage:111:"
            + (currentPage < subChildCount - 1) + "  " + currentPage
            + "   " + (subChildCount - 1));
    if (currentPage < subChildCount - 1) {
        currentPage += Zoom(intNext);
        if (currentPage > subChildCount - 1) {
            currentPage = subChildCount - 1;
        }
        /* currentPage++; */Log.i("hx",
                "MyHorizontalScrollView---->smoothScrollToNextPage:222:"
                        + currentPage + "   " + pointList.get(currentPage));
        smoothScrollTo(pointList.get(currentPage), 0);
    }
}

private int Zoom(int intNext) {
    int page = 0;
    for (int i = 0; i < pointList.size(); i++) {
        if (!pointList.isEmpty()) {
            Log.i("hx", "pointList:isEmpty" + i);
        if ((pointList.get(i + 1) + 20 > intNext)
                && (intNext > pointList.get(i) + 20)) {
            Log.i("hx", "pointList.get(i + 1) : " + pointList.get(i + 1) + 20 +" : "+ intNext);

// if(i != 0){
page = i + 1;
Log.i("hx", "page = i + 1 : " + page);
// }
break;
}
}
}
Log.i("hx", "MyHorizontalScrollView---->Zoomm:" + intNext + " "
+ page);
return page;
}
说我Zoom方法里面的if那一行的list越界了 请问如何做处理

2个回答

u011686167
u011686167   2017.01.07 14:08

if ((pointList.get(i + 1) + 20 > intNext),这里i+1导致的。因为数组下标是从0开始的,在遍历时,到了最后一个,pointList.get(i + 1) 变成调用数组最后一个的下一个,引起越界。如果你一定要这样写,需要加上判断是否为数组最后一个。如果不是最后一个,才调用if这个语句。

LIJIAN0927
LIJIAN0927 那我怎么判断他是不是最后一个?
11 个月之前 回复
u011686167
u011686167   2017.01.07 19:43

if(i != (pointList.size()-1)) { //不是最后一个
...
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!