我在Android工作室有一个libgdx项目。在AndroidLauncher类中,我启动一个名为ClockActivity的ApplicationAdapter,并注册一个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;
}
}
}
ClockActivity扩展了ApplicationAdapter类,其作用是在屏幕上显示当前系统时间。
public class ClockActivity extends ApplicationAdapter {
@Override
public void create () {
Gdx.graphics.setContinuousRendering(true);
}
@Override
public void render() {
// my job is to display time
}
}
当我在我的Android手机上运行时,我试图平移希望调整亮度。但是,应用程序崩溃时出现以下异常。
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)
我无法理解CalledFromWrongThreadException错误。请帮忙。
答案 0 :(得分:0)
查看答案here。在你的pan
方法中,你不能对你的布局做任何事情,因为(我认为)它是从GDX线程调用的,而不是Android线程。您必须使用处理程序在Android线程上执行布局代码。
也许尝试这样的事情:
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);
}
};
现在你的pan方法:
public boolean pan(float x, ...) {
handler.post(panUI);
return false;
}