gpt4 book ai didi

java - 单击 setOnTouchListener 时出现空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:25 24 4
gpt4 key购买 nike

我试图实现一个 daragble 相对布局。所以我实现了这段代码:-

    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;
}
});

当我拖动 View 时,它工作正常,但是当我单击时,我收到空指针异常。

 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)

我在这里做错了什么?

最佳答案

声明全局 boolean 值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;
}
}

});

先生,请检查格式和关闭,我这样做时没有记住这一点,如果这也有帮助,请告诉我

关于java - 单击 setOnTouchListener 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28156203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com