Android extension thread exception

I’m following this tutorial for creating native extensions. I have an empty openfl project running only extension static method setBrightness, however I get an error:

W/System.err( 4796): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
W/System.err( 4796):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4317)
W/System.err( 4796):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:879)
W/System.err( 4796):    at android.view.View.requestLayout(View.java:12817)
W/System.err( 4796):    at android.view.View.setLayoutParams(View.java:8324)
W/System.err( 4796):    at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:312)
W/System.err( 4796):    at android.view.WindowManagerImpl$CompatModeWrapper.updateViewLayout(WindowManagerImpl.java:145)
W/System.err( 4796):    at android.app.Activity.onWindowAttributesChanged(Activity.java:2240)
W/System.err( 4796):    at android.view.Window.setAttributes(Window.java:785)
W/System.err( 4796):    at org.haxe.extension.SetBrightness.setBrightness(SetBrightness.java:53)
W/System.err( 4796):    at org.haxe.HXCPP.main(Native Method)
W/System.err( 4796):    at org.haxe.HXCPP.main(Native Method)
W/System.err( 4796):    at org.haxe.HXCPP.run(HXCPP.java:23)
W/System.err( 4796):    at org.libsdl.app.SDLMain.run(SDLActivity.java:1030)
W/System.err( 4796):    at java.lang.Thread.run(Thread.java:856)

How to get to main thread if Extension.mainActivity looks like it doesn’t run on it?

Another question is about lime trace android - currently it filters everything which isn’t Log.i("trace", $1, $2). Running with -v is fine, but it shows a lot of unimportant info. All I want is to see unhandled exceptions as well in addition to traces. Any ideas?

Openfl: git version as of 2016-12-29
Lime: git version as of 2016-12-29
Phone OS: Android 4.0.4

Ok, some google-fu allowed me to get it working:

public static void setBrightness(final float brightness) {
    Extension.mainActivity.runOnUiThread(new Runnable() 
    {
        @Override
        public void run() 
        {
            WindowManager.LayoutParams layout = Extension.mainActivity.getWindow().getAttributes();
            layout.screenBrightness = brightness;
            Extension.mainActivity.getWindow().setAttributes(layout);
        }
    });
}

Is this the correct way?

The question about filtering logs still stands though

Yes, that’s one correct way. You can also use Extension.callbackHandler.post(), but as far as I know, neither is better than the other.

Sorry for the trouble! I’ll add a note to the blog post.

Edit: note added. How’s the explanation look?

1 Like