我正在尝试注册SensorEventListener,但我的侦听器输入错误。
以下是我尝试过的:
;; listener
(gen-class
:name com.spython.pushupcounter.main.sensor-listener
:implements [android.hardware.SensorEventListener]
:prefix "-"
:methods [[onAccuracyChanged [android.hardware.Sensor Integer] void]
[onSensorChanged [android.hardware.SensorEvent] void]])
(def listener com.spython.pushupcounter.main.sensor-listener)
(.registerListener sensor-manager listener proximitySensor 2)看来我得把listener投给SensorEventListener了,对吧?
我该怎么做?
发布于 2014-07-07 02:40:20
与类名匹配的符号,如com.spython.pushupcounter.main.sensor-listener解析为java.lang.Class实例。所以你的listener是一个Class,这不是你想要的。它应该是com.spython.pushupcounter.main.sensor-listener的一个实例。可以使用标准实例化语法(com.spython.pushupcounter.main.sensor-listener.)来创建实例--请注意.在new的末尾语法糖。但是即使你修复了这个问题,代码也不能工作。用这种方式使用(gen-class)是很棘手的。它只在使用AOT编译时生成类,而不使用AOT编译。您还必须为SensorEventListener方法提供实现。
更好的方法是使用(reify),它返回实现所需接口的对象。例如:
(defn listener []
(reify
android.hardware.SensorEventListener
(onAccuracyChanged [_ sensor accuracy]
(comment onAccuracyChanged implementation here))
(onSensorChanged [_ event]
(comment onSensorChanged implementation here))))
(.registerListener sensor-manager (listener) proximitySensor 2)https://stackoverflow.com/questions/24593886
复制相似问题