简体   繁体   English

Android和ArrayLIst Size返回0

[英]Android and ArrayLIst Size returns 0

Strange problem I'm having here. 我在这里有奇怪的问题。 I add to an arraylist when the user clicks on the view. 当用户单击视图时,我将添加到arraylist。 I get the position and add it to an ArrayList of coordinates. 我得到的位置,并将其添加到坐标的ArrayList。 I then draw a circle on the canvas where the coordinates says to do so. 然后,我在画布上画一个圆,坐标表示要这样做。 The Size check in onDraw always returns 0. onDraw中的Size校验始终返回0。

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

OnTouchEvent 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);
    }
}

How are you expecting to get a static field with this ? 你如何期望得到一个静态字段与this But assuming that's just some typo, try adding some logging: 但是假设这只是一些输入错误,请尝试添加一些日志记录:

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;

And in your onDraw: 并在您的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM