Android和ArrayLIst Size返回0

时间:2012-08-24 05:13:14

标签: java android arraylist

奇怪的问题我在这里。当用户点击视图时,我会添加一个arraylist。我得到位置并将其添加到坐标的ArrayList。然后我在画布上绘制一个圆圈,坐标表示这样做。 onDraw中的大小检查始终返回0.

private static ArrayList<Coordinate> coords = new ArrayList<Coordinate>();

的onTouchEvent

...
case MotionEvent.ACTION_POINTER_UP: {
    mLastTouchX = event.getX();
    mLastTouchY = event.getY();
    this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
    break;
...

的OnDraw

protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    for(int i = 0; i < this.coords.size(); i++) {
        canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
    }
}

1 个答案:

答案 0 :(得分:1)

您希望如何获得this的静态字段?但假设这只是一些错字,请尝试添加一些日志记录:

case MotionEvent.ACTION_POINTER_UP: {
    mLastTouchX = event.getX();
    mLastTouchY = event.getY();
    this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
    System.out.println("Added a coordinate; new size: " + coords.size());//to see if we are adding it
    break;

在你的onDraw中:

System.out.println(coords);//Just to see what all is in it
for(int i = 0; i < this.coords.size(); i++) {
    canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
}