0

I have a libgdx project in Android studio. In the AndroidLauncher class i am starting a ApplicationAdapter called ClockActivity and also registering a GuestureDetector.

    public class AndroidLauncher extends AndroidApplication {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        ClockActivity clockActivity = new ClockActivity();
        Gdx.input.setInputProcessor(new GestureDetector(new GestureHandler()));

    }


class GestureHandler implements GestureDetector.GestureListener {

            @Override
        public boolean pan(float x, float y, float deltaX, float deltaY) {

            WindowManager.LayoutParams layout;

            Gdx.app.log("LOG:", "pan" + deltaY);

            layout = getWindow().getAttributes();
            float currentValue = layout.screenBrightness;
            layout.screenBrightness = currentValue - 10;
            getWindow().setAttributes(layout);
            return false;
        }


}
}

The ClockActivity extends ApplicationAdapter class and its job is to display current system time on screen.

public class ClockActivity extends ApplicationAdapter {
@Override
public void create () {
    Gdx.graphics.setContinuousRendering(true);
}
@Override
public void render() {
    // my job is to display time
}
}

When i run on my Android phone and I try to pan hoping to adjust the brightness. However, application crashes with the following exception.

E/AndroidRuntime: FATAL EXCEPTION: GLThread 42617
              Process: com.mygdx.clock, PID: 25614
              android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6293)
                  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:900)
                  at android.view.View.requestLayout(View.java:17378)
                  at android.view.View.setLayoutParams(View.java:11373)
                  at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:302)
                  at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:74)
                  at android.app.Activity.onWindowAttributesChanged(Activity.java:2608)
                  at android.view.Window.dispatchWindowAttributesChanged(Window.java:832)
                  at com.android.internal.policy.impl.PhoneWindow.dispatchWindowAttributesChanged(PhoneWindow.java:3879)
                  at android.view.Window.setAttributes(Window.java:863)
                  at com.mygdx.clock.AndroidLauncher$GestureHandler.pan(AndroidLauncher.java:99)
                  at com.badlogic.gdx.input.GestureDetector.touchDragged(GestureDetector.java:159)
                  at com.badlogic.gdx.input.GestureDetector.touchDragged(GestureDetector.java:126)
                  at com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:382)
                  at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:457)
                  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
                  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

I am not able to understand the CalledFromWrongThreadException error. Please help.

antman
  • 317
  • 6
  • 16

1 Answers1

0

See the answer here. Inside your pan method, you cannot do anything with your layout, since (I think) it is called from the GDX thread, and not the Android thread. You will have to use a handler to execute the layout code on the Android thread.

Maybe try something like this:

private final Handler handler = new Handler(Looper.getMainLooper());
private final Runnable panUI = new Runnable() {
    @Override
    public void run() {
        layout = getWindow().getAttributes();
        float currentValue = layout.screenBrightness;
        layout.screenBrightness = currentValue - 10;
        getWindow().setAttributes(layout);
    }
};

Now in your pan method:

public boolean pan(float x, ...) {
    handler.post(panUI);
    return false;
}
Community
  • 1
  • 1
Matthew Tory
  • 1,306
  • 2
  • 16
  • 30