0

我有一个基于本教程的项目,其中用户旋转轮子并且数字的值int days根据轮子的当前位置发生变化,现在我想ImageView circle根据这个数字的值显示或隐藏另一个图像,但是它抛出NullPointerException

这是我的onTouch活动,对不起我的英语不好

@Override
public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            // reset the touched quadrants
            for (int i = 0; i < quadrantTouched.length; i++) {
                quadrantTouched[i] = false;
            }
            allowRotating = false;
            startAngle = getAngle(event.getX(), event.getY());
            break;

        case MotionEvent.ACTION_MOVE:
            double currentAngle = getAngle(event.getX(), event.getY());
            rotateDialer((float) (startAngle - currentAngle));
            startAngle = currentAngle;
            days = ((int) currentAngle / 12) + 1;
            String test = Integer.toString(days);
            tvnumber.setText(test);

            switch (days) {
                case 1:
                    circle.setVisibility(View.INVISIBLE);
                    break;
                case 3:
                    circle.setVisibility(View.VISIBLE);
                    break;
            }
            break;

        case MotionEvent.ACTION_UP:
            allowRotating = false;
            break;
    }
}

我的日志猫 在此处输入图像描述

4

3 回答 3

5

您需要初始化circle视图,最有可能使用该findViewById()方法

于 2014-09-17T07:09:21.633 回答
4

在第 194 行开关盒之前尝试这样的事情:

ImageView circle = (ImageView) view.findViewById(R.id.your_iamgeview);

这是您的整个代码:

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
        // reset the touched quadrants
        for (int i = 0; i < quadrantTouched.length; i++) {
            quadrantTouched[i] = false;
        }
        allowRotating = false;
        startAngle = getAngle(event.getX(), event.getY());
        break;

    case MotionEvent.ACTION_MOVE:
        double currentAngle = getAngle(event.getX(), event.getY());
        rotateDialer((float) (startAngle - currentAngle));
        startAngle = currentAngle;
        days = ((int) currentAngle / 12) + 1;
        String test = Integer.toString(days);
        tvnumber.setText(test);
        ImageView circle = (ImageView) v.findViewById(R.id.your_iamgeview); //<-- Add this
        switch (days) {
            case 1:
                circle.setVisibility(View.INVISIBLE);
                break;
            case 3:
                circle.setVisibility(View.VISIBLE);
                break;
        }
        break;

    case MotionEvent.ACTION_UP:
        allowRotating = false;
        break;
}

}

于 2014-09-17T07:10:12.323 回答
3

你是这样定义圆的吗?

ImageView circle = (ImageView) findViewById(R.id.xxx);
于 2014-09-17T07:09:48.323 回答