Search code examples
javaandroidnullpointerexceptionontouchlistenertouch-event

Can't implement onTouchListener


Please help me. I can't implement onTouchListener. I have declared view from my Main activity in my Touch class. But i have an NullPointerException in result.

My Main activity code:

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

                }
            }
        };

    }

}

My Touch class:

public class XyloTouch extends Main implements 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;

}

}

Logs

"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()....."

Please help me, i realy tired to try fix it.


Solution

  • You have a serious design issue here. You don't need to extend the Main Class by your XyloTouch class. You could simply use your Main class and make it implement the OnTouchListener.

    The problem here is your XyloTouch class does not have valid reference to instance variables like c_lL as they are not set by the class itself when you do

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

    Easiest solution is to make your Main class implement OnTouchListener and use it as the listener. Or pass the reference of the instance variables of the Main class to XyloTouch, but that is a bad design.

    One solution you could also use is to turn your XyloTouch class to an inner class like this:

        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.
        }
    
    }