java - Android 和 ArrayLIst Size 返回 0

标签 java android arraylist

我在这里遇到了奇怪的问题。当用户单击 View 时,我添加到数组列表。我获取位置并将其添加到坐标 ArrayList 中。然后,我在 Canvas 上按照坐标指示绘制一个圆圈。 onDraw 中的 Size 检查始终返回 0。

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

触摸事件

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

绘图

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

最佳答案

您希望如何使用 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);
}

关于java - Android 和 ArrayLIst Size 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103460/

相关文章:

java - 多点触控跨平台 java 应用程序(Windows、Mac 和 Linux(Ubuntu)(可能是 Android))

Java : ProgressBar does not display when click on the button

带有 odatajclient 库的 android 项目会出现运行时错误

android - 以编程方式布局android fragment

java - 如何先按字母顺序排序ArrayList,然后按数字排序

重新分配 Java session 对象

Java:合取不等式的最少运算次数?

java - MySQL 中保存的日期无效

java - 如何获取android中特定区域的坐标?

java - Android 在 Class Object 中添加 ArrayList<Object> 作为变量