报错:$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
这个错误是因为在 主线程之外的线程中进行了修改主界面UI的操作。
问题:我目前遇到的问题是在子线程中想对屏幕亮度进行修改导致上面的报错。
解决方法:在主线程中加一个handle
public void setBrightness(float brightnessRatio) { //改变亮度
WindowManager.LayoutParams layoutpars = Window.getAttributes();
lp.screenBrightness = brightnessRatio;
Window.setAttributes(lp);
}
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
mScreenBrightnessManager.setBrightness((float)msg.what/1000.0f);
}
};
然后在子线程中对其进行调用:
mHandler.sendEmptyMessageDelayed((int)(ratio*1000),0);
因为sendEmptyMessageDelayed(int,long)第一个参数为int,所以得进行一些转换。