I tried to build an app with the android twisti module (see Forging Titanium ep.14,15,16). had to modify android code to fit 1.8 module requirement. The app crashed when sending the update event to the js side. :
D/Twisti(1465): (main) [1,53] fireEvent x:0.0 y:0.0 z:10.623871 D/Twisti(1465): (main) [0,53] fireEvent az:-3.1415927 pi:1.5707964 ro:-0.0 W/dalvikvm(1465): threadid=7: thread exiting with uncaught exception (group=0x2aacc8a0) E/TiApplication(1465): (KrollRuntimeThread) [2,55] Sending event: exception on thread: KrollRuntimeThread msg:java.lang.ClassCastException: org.appcelerator.kroll.runtime.v8.V8Function; Titanium 2.1.3,2012/10/02 16:16,15997d0 E/TiApplication(1465): java.lang.ClassCastException: org.appcelerator.kroll.runtime.v8.V8Function E/TiApplication(1465): at org.appcelerator.kroll.KrollProxy.onEventFired(KrollProxy.java:960) E/TiApplication(1465): at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:633) E/TiApplication(1465): at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:829) E/TiApplication(1465): at android.os.Handler.dispatchMessage(Handler.java:95) E/TiApplication(1465): at android.os.Looper.loop(Looper.java:123) E/TiApplication(1465): at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:109)
The 2 first traces show that the twisti module is going to send an update event with correct data.
@Kroll.module(name="Twisti", id="ti.twisti") public class TwistiModule extends KrollModule implements SensorEventListener { private static final String LCAT = "Twisti"; private static final boolean DBG = TiConfig.LOGD; private static final String EVENT_UPDATE = "update"; private boolean accelerometerRegistered = false; private float[] accelValues = null; private float[] magValues = null; private float[] R = new float[16]; private float[] I = new float[16]; private float[] outR = new float[16]; private float[] orientation = new float[3]; private boolean isReady = false; public TwistiModule() { super(); Log.d(LCAT, "TwistiModule !"); } @Kroll.method @Override public int addEventListener(String eventName, KrollEventCallback listener) { Log.d(LCAT, "addEventListener: " + eventName); if (!accelerometerRegistered) { if (EVENT_UPDATE.equals(eventName)) { Log.d(LCAT, "registerListener "); TiSensorHelper.registerListener(Sensor.TYPE_ACCELEROMETER, this, SensorManager.SENSOR_DELAY_UI); TiSensorHelper.registerListener(Sensor.TYPE_MAGNETIC_FIELD, this, SensorManager.SENSOR_DELAY_UI); accelerometerRegistered = true; } } return super.addEventListener(eventName, listener); } @Kroll.method @Override public void removeEventListener(String eventName, int listener) { if (accelerometerRegistered) { if (EVENT_UPDATE.equals(eventName)) { TiSensorHelper.unregisterListener(Sensor.TYPE_ACCELEROMETER, this); TiSensorHelper.unregisterListener(Sensor.TYPE_MAGNETIC_FIELD, this); accelerometerRegistered = false; } } super.removeEventListener(eventName, listener); } public void onAccuracyChanged(Sensor sensor, int accuracy) { Log.d(LCAT, "onAccuracyChanged: " + accuracy); } public void onSensorChanged(SensorEvent event) { Log.d(LCAT, "onSensorChanged: " + event.sensor.getType()); switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: accelValues = event.values.clone(); break; case Sensor.TYPE_MAGNETIC_FIELD: magValues = event.values.clone(); isReady = true; break; } if (accelValues != null && magValues != null && isReady) { isReady = false; SensorManager.getRotationMatrix(R, I, accelValues, magValues); SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR); SensorManager.getOrientation(outR, orientation); Log.d(LCAT, "fireEvent x:" + accelValues[0]+" y:"+ accelValues[1]+" z:"+ accelValues[2]); Log.d(LCAT, "fireEvent az:" + orientation[0]+" pi:"+ orientation[1]+" ro:"+ orientation[2]); KrollDict data = new KrollDict(); data.put("x", accelValues[0]); data.put("y", accelValues[1]); data.put("z", accelValues[2]); data.put("azimuth", orientation[0]); data.put("pitch", orientation[1]); data.put("roll", orientation[2]); fireEvent(EVENT_UPDATE, data); } } }environment : OSX 10.6.8, Ti SDK 2.1.3.GA, android 2.2 device, twisti module built for android-8 platform
Any idea ?
1 Answer
I realize that overriding the addEventListener method breaks something : the event listener is lost (hasListeners("update") is false) and firing this 'update' event makes the app crash. I made another native module from moddevguide project using KromDemoProxy. If I override the addEventListener, the "demo event" listener disappears and the app crashes...
Your Answer
Think you can help? Login to answer this question!