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(
W/System.err( 4796):    at android.view.ViewRootImpl.requestLayout(
W/System.err( 4796):    at android.view.View.requestLayout(
W/System.err( 4796):    at android.view.View.setLayoutParams(
W/System.err( 4796):    at android.view.WindowManagerImpl.updateViewLayout(
W/System.err( 4796):    at android.view.WindowManagerImpl$CompatModeWrapper.updateViewLayout(
W/System.err( 4796):    at
W/System.err( 4796):    at android.view.Window.setAttributes(
W/System.err( 4796):    at org.haxe.extension.SetBrightness.setBrightness(
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
W/System.err( 4796):    at
W/System.err( 4796):    at

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() 
        public void run() 
            WindowManager.LayoutParams layout = Extension.mainActivity.getWindow().getAttributes();
            layout.screenBrightness = brightness;

Is this the correct way?

The question about filtering logs still stands though

Yes, that’s one correct way. You can also use, 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