为什么会onTouch()生成致命异常返回false时? [英] Why would onTouch() generate FATAL EXCEPTION when returning false?

查看:260
本文介绍了为什么会onTouch()生成致命异常返回false时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的MotionEvent演习,我要做的就是记录事件的参数。我这样做,通过实施 OnTouchListener 并重写 onTouch()中的主要活动。

有关一个按钮,我回真正是在onTouch的末尾()。没有问题存在。

有关另一个按钮,我回是在onTouch的末尾()。这是它变得有趣。仅仅通过返回假,一个致命异常被抛出一个微小的暗示,我真的不明白:!回收两次

这是什么意思?

更新:每在下面的评论的建议,我从LogCat中添加异常的详细信息:

  10月3日至8日:35:14.275:ERROR / AndroidRuntime(521):致命异常:主要
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):了java.lang.RuntimeException:MotionEvent {405215b0行动= 0 X = 66.0 Y = 78.0 pressure = 1.0大小= 0.0}回收两次!
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在android.view.MotionEvent.recycle(MotionEvent.java:659)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在android.os.Handler.dispatchMessage(Handler.java:99)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在android.os.Looper.loop(Looper.java:123)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在android.app.ActivityThread.main(ActivityThread.java:3647)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在java.lang.reflect.Method.invokeNative(本机方法)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在java.lang.reflect.Method.invoke(Method.java:507)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
十月3日至八日:35:14.275:ERROR / AndroidRuntime(521):在dalvik.system.NativeStart.main(本机方法)


解决方案

您回收,然后通过

该框架将假设MotionEvent仍然是有效的,如果你返回false,并将尽自己的加工它。

文档


  

回收的MotionEvent,要被重新使用
  由更高版本的调用者。调用此之后
  工作,你千万不要对碰
  再次事件。


I wrote a simple MotionEvent exercise in which all I do is log the parameters of the event. I do that by implementing OnTouchListener and overriding onTouch() in the main activity.

For one button, I return true at the end of onTouch(). No problems there.

For another button, I return false at the end of onTouch(). This is where it gets interesting. By mere returning 'false', a FATAL EXCEPTION is thrown with a tiny hint which I don't really understand: "recycled twice!".

What does this mean?

Update: per the suggestion in the comment below, I am adding the details of the exception from LogCat:

03-08 10:35:14.275: ERROR/AndroidRuntime(521): FATAL EXCEPTION: main
03-08 10:35:14.275: ERROR/AndroidRuntime(521): java.lang.RuntimeException: MotionEvent{405215b0 action=0 x=66.0 y=78.0 pressure=1.0 size=0.0} recycled twice!
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at android.view.MotionEvent.recycle(MotionEvent.java:659)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at android.os.Looper.loop(Looper.java:123)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at android.app.ActivityThread.main(ActivityThread.java:3647)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at java.lang.reflect.Method.invoke(Method.java:507)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-08 10:35:14.275: ERROR/AndroidRuntime(521):     at dalvik.system.NativeStart.main(Native Method)

解决方案

Are you recycling and then passing false?

The framework will assume that the MotionEvent is still valid if you return false and will do its own processing with it.

From the docs:

Recycle the MotionEvent, to be re-used by a later caller. After calling this function you must not ever touch the event again.

这篇关于为什么会onTouch()生成致命异常返回false时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆