LIJIAN0927 2017-01-07 02:35 采纳率: 0%
浏览 2313

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条回答 默认 最新

  • 徐福记456 2017-01-07 06:08
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗