单击setOnTouchListener时出现空指针异常

时间:2015-01-26 18:17:43

标签: java android

我试图实现一个可扩展的相对布局。所以我实现了这个代码: -

    body.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // pass all touch events to the implementation
            boolean consumed = false;
            // handle move and bring to front
            consumed = context.onTouchHandleMove(id, Window.this, v, event)|| consumed;
            // alert implementation
        consumed = context.onTouchBody(id, Window.this, v, event)|| consumed;
        Log.e("Now touched ","Now in touch   "+consumed);
            return consumed;
        }
    });

当我拖动我的视图时,它完美地工作,但是当我点击时我得到空指针异常。

 01-26 13:45:21.260: E/AndroidRuntime(980): FATAL EXCEPTION: main
    01-26 13:45:21.260: E/AndroidRuntime(980):    java.lang.NullPointerException
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.View.onAttachedToWindow(View.java:11709)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.View.dispatchAttachedToWindow(View.java:12125)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2450)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.Choreographer.doFrame(Choreographer.java:532)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.os.Handler.handleCallback(Handler.java:730)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.os.Handler.dispatchMessage(Handler.java:92)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.os.Looper.loop(Looper.java:137)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at android.app.ActivityThread.main(ActivityThread.java:5103)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at java.lang.reflect.Method.invoke(Method.java:525)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    01-26 13:45:21.260: E/AndroidRuntime(980):  at dalvik.system.NativeStart.main(Native Method)

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

声明全局布尔值boolean clickToBe = false

body.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // pass all touch events to the implementation
        if(event.getAction() == MotionEvent.ACTION_DOWN){
           clickToBe = true;
         }else if(event.getAction() == MotionEvent.ACTION_UP){
               if(clickToBe)
                    return false; v.performClick(); // if you want onclick listeners
           } else

        if(event.getAction() == MotionEvent.ACTION_MOVE){ 
        clickToBe = false               
        boolean consumed = false;
        // handle move and bring to front
        consumed = context.onTouchHandleMove(id, Window.this, v, event)|| consumed;
        // alert implementation
    consumed = context.onTouchBody(id, Window.this, v, event)|| consumed;
    Log.e("Now touched ","Now in touch   "+consumed);
        return consumed;
     }   
    }

});

先生,请检查格式化和关闭,我是在没有记住这一点的情况下完成的,如果这也有帮助,请告诉我

答案 1 :(得分:0)

试试这个:

context =this;
body.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// pass all touch events to the implementation
boolean consumed = false;
// handle move and bring to front
if (context==null){
return;}
consumed = context.onTouchHandleMove(id, Window.this, v, event)|| consumed;
// alert implementation
consumed = context.onTouchBody(id, Window.this, v, event)|| consumed;
Log.e("Now touched ","Now in touch "+consumed);
return consumed;
}
});