首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法实现onTouchListener

无法实现onTouchListener
EN

Stack Overflow用户
提问于 2014-12-25 17:44:00
回答 1查看 365关注 0票数 0

请帮帮我。我不能实现onTouchListener。我已经在我的触觉课上宣布了我的主要活动的观点。但是我得到了一个NullPointerException。

我的主要活动代码:

代码语言:javascript
复制
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扩展主实现OnTouchListener {

代码语言:javascript
复制
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;

}

}

日志

代码语言:javascript
复制
"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()....."

请帮帮我,我真的很累了,想把它修好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-25 17:59:45

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

这里的问题是您的XyloTouch类没有对实例变量(如c_lL )的有效引用,因为这些变量不是由类本身设置的。

代码语言:javascript
复制
XyloTouch xyloTouch = new XyloTouch();
    main_lL.setOnTouchListener(xyloTouch);

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

您还可以使用的一个解决方案是将XyloTouch类转换为如下所示的内部类:

代码语言:javascript
复制
    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.
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27649461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档