无法实现onTouchListener

时间:2014-12-25 17:44:01

标签: java android nullpointerexception ontouchlistener ontouchevent

请帮帮我。 我无法实现onTouchListener。 我在Touch类的主要活动中声明了视图。但是我在结果中有一个NullPointerException。

我的主要活动代码:

public class Main extends Activity{

    LinearLayout main_lL, c_lL, d_lL, e_lL, f_lL, g_lL, a_lL, b_lL, c1_lL;
    ImageView c_iV, d_iV, e_iV, f_iV, g_iV, a_iV, b_iV, c1_iV;
    int upPI = 0;
    int downPI = 0;
    boolean inTouch = false;
    Handler h;
    public Notes notes;

    final int STATUS_NOT_TOUCHED = 0;
    ... ... ... ... ... 
    ... ... ... ... ...


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_lL = (LinearLayout) findViewById(R.id.mainLinearLayout);

        XyloTouch xyloTouch = new XyloTouch();
        main_lL.setOnTouchListener(xyloTouch);


        c_lL = (LinearLayout) findViewById(R.id.c_lL);
        d_lL = (LinearLayout) findViewById(R.id.d_lL);
        e_lL = (LinearLayout) findViewById(R.id.e_lL);
        f_lL = (LinearLayout) findViewById(R.id.f_lL);
        g_lL = (LinearLayout) findViewById(R.id.g_lL);
        a_lL = (LinearLayout) findViewById(R.id.a_lL);
        b_lL = (LinearLayout) findViewById(R.id.b_lL);
        c1_lL = (LinearLayout) findViewById(R.id.c1_lL);

        c_iV = (ImageView) findViewById(R.id.c_iV);
        d_iV = (ImageView) findViewById(R.id.d_iV);
        e_iV = (ImageView) findViewById(R.id.e_iV);
        f_iV = (ImageView) findViewById(R.id.f_iV);
        g_iV = (ImageView) findViewById(R.id.g_iV);
        a_iV = (ImageView) findViewById(R.id.a_iV);
        b_iV = (ImageView) findViewById(R.id.b_iV);
        c1_iV = (ImageView) findViewById(R.id.c1_iV);

        notes = new Notes(this);

         h = new Handler() {
            public void handleMessage(android.os.Message msg) {

                switch (msg.what) {
                    case STATUS_NOT_TOUCHED:
                      //Do my stuff  
                    break;
                    case STATUS_C_TOUCHED:
                        //Do my stuff 

                        break;
                    case STATUS_C_NOT_TOUCHED:
                        //Do my stuff 

                        break;
                   // other cases

                }
            }
        };

    }

}

我的触摸课程:

公共类XyloTouch扩展Main实现OnTouchListener {

public XyloTouch(){
    super();
}

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



    final int actionMask = event.getActionMasked();
    final int pointerCount = event.getPointerCount();
    final int pointerIndex = event.getActionIndex();
    final int pointerId = event.getPointerId(pointerIndex);

    switch (actionMask & event.getAction())

    {
        case MotionEvent.ACTION_DOWN:

        case MotionEvent.ACTION_POINTER_DOWN:

            for (int i = 0; i < pointerCount; i++) {


                if (event.getX(pointerIndex) > c_lL.getX() + c_lL.getWidth() / 8 && event.getX(pointerIndex) < c_lL.getX() + c_lL.getWidth() - c_lL.getWidth() / 8
                        && event.getY(pointerIndex) > c_iV.getY() + c_iV.getHeight() / 7 && event.getY(pointerIndex) < c_iV.getY() + c_iV.getHeight() - c_iV.getHeight() / 7) {

                    c_touched = true;
                    c_touched2 = true;

                }

               ... ... ... ...
            break;


        case MotionEvent.ACTION_UP:

          ... ... ... ...
            break;


        case MotionEvent.ACTION_POINTER_UP:

            ... ... ... ...
            break;

        case MotionEvent.ACTION_MOVE:


           ... ... ... ...
            break;

    }


    if (c_touched) {
        h.sendEmptyMessage(STATUS_C_TOUCHED);

    } else {
        h.sendEmptyMessage(STATUS_C_NOT_TOUCHED);
    }
   ... ... ... ...

    return true;

}

}

记录

"at dalvik.system.NativeStart.main(Native Method)
12-26 02:14:53.861      715-715/com....... E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com........XyloTouch.onTouch(XyloTouch.java:45)"
in my XyloTouch.onTouch in case event.ACTION_DOWN at the line - "if     (event.getX(pointerIndex) > c_lL.getX() + c_lL.getWidth() / 8 && event.getX(pointerIndex) < c_lL.getX() + c_lL.getWidth() - c_lL.getWidth()....."

请帮助我,我真的厌倦了尝试解决它。​​

1 个答案:

答案 0 :(得分:0)

这里有一个严重的设计问题。您不需要通过XyloTouch类扩展Main Class。您可以简单地使用Main类并使其实现OnTouchListener。

这里的问题是你的XyloTouch类没有像 c_lL 这样的实例变量的有效引用,因为当你这样做时它们不是由类本身设置的

XyloTouch xyloTouch = new XyloTouch();
    main_lL.setOnTouchListener(xyloTouch);

最简单的解决方案是让您的Main类实现OnTouchListener并将其用作侦听器。或者将Main类的实例变量的引用传递给XyloTouch,但这是一个糟糕的设计。

您还可以使用的一种解决方案是将XyloTouch类转换为这样的内部类:

    public class Main extends Activity{

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            main_lL = (LinearLayout) findViewById(R.id.mainLinearLayout);

            XyloTouch xyloTouch = new XyloTouch();
            main_lL.setOnTouchListener(xyloTouch);
            ........
        }

    .....

    private class XyloTouch implements OnTouchListener{
    // exact code inside of Xylo class goes here. It can access the instance variables of Main as 
    // its an inner class and Main is a parent class.
    }

}