[英]Why setBackgroundColor on a layout sometimes crash my app
I'm building an application that can send and received OSC.我正在构建一个可以发送和接收 OSC 的应用程序。 I'm using JavaOSC, it feets perfectly my needs.
我正在使用 JavaOSC,它完全符合我的需求。
I'm sending color value in OSC message, and receiving them also.我在 OSC 消息中发送颜色值,也接收它们。
I send the color from a colorpicker based on this color picker http://code.google.com/p/color-picker-view/ it works great.我从基于此颜色选择器的颜色选择器发送颜色http://code.google.com/p/color-picker-view/效果很好。
My problem is :我的问题是:
private ColorPickerView.OnColorChangedListener colorListener = new ColorPickerView.OnColorChangedListener(){
private ColorPickerView.OnColorChangedListener colorListener = new ColorPickerView.OnColorChangedListener(){
@Override
public void onColorChanged(int color) {
caller.sending("color", color);
mScreen.setBackgroundColor(color);
}
};
where mScreen is a LinearLyout其中 mScreen 是 LinearLyout
mScreen = (LinearLayout) findViewById(R.id.myScreen); mScreen = (LinearLayout) findViewById(R.id.myScreen);
It works as expected.它按预期工作。
My listener is我的听众是
OSCListener listener = new OSCListener() {
OSCListener 监听器 = 新 OSCListener() {
public void acceptMessage(java.util.Date time, OSCMessage message) {
//en cas de message vide
if (message.getArguments().length == 0) return;
//sinon on recupere les elements et on les tries
Object[] args = message.getArguments();
if (args[0].toString().contains("alpha")) Log.i("receiver osc", "Message received!");
//Instructions
if (args[0].toString().contains("color")) {
int color = (Integer)args[1];
//mColorPickerView.setColor((Integer)args[1]);
mScreen.setBackgroundColor(color);
}
else return;
}
You can find the crash report below.您可以在下面找到崩溃报告。 Do the function setBackgroundColor need to be in a particular function to work properly (in a onClick function for example) or when it redraws something on the screen ?
函数 setBackgroundColor 是否需要在特定函数中才能正常工作(例如在 onClick 函数中)或在屏幕上重绘某些内容时?
Crash report崩溃报告
08-05 15:18:15.035: WARN/dalvikvm(18083): threadid=7: thread exiting with uncaught exception (group=0x4001d7d0) ERROR/AndroidRuntime(18083): FATAL EXCEPTION: Thread-8 ERROR/AndroidRuntime(18083): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-05 15:18:15.035: WARN/dalvikvm(18083): threadid=7: 线程退出时出现未捕获的异常 (group=0x4001d7d0) 错误/AndroidRuntime(18083): 致命异常: Thread-8 错误/Android808Runtime(18083):1 android.view.ViewRoot$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。 ERROR/AndroidRuntime(18083): at android.view.ViewRoot.checkThread(ViewRoot.java:2811) ERROR/AndroidRuntime(18083): at android.view.ViewRoot.requestLayout(ViewRoot.java:594) ERROR/AndroidRuntime(18083): at android.view.View.requestLayout(View.java:8180) ERROR/AndroidRuntime(18083): at android.view.View.requestLayout(View.java:8180) ERROR/AndroidRuntime(18083): at android.view.View.requestLayout(View.java:8180) ERROR/AndroidRuntime(18083): at android.view.View.setBackgroundDrawable(View.java:7535) ERROR/AndroidRuntime(18083): at android.view.View.setBackgroundColor(View.java:7429) ERROR/AndroidRuntime(18083): at com.taprik.Remote.RemoteMain$4.acceptMessage(RemoteMain.java:202) ERROR/AndroidRuntime(18083): at com.illposed.osc.utility.OSCPacketDispatcher.dispatchMessage(Unknown Source) ERROR/AndroidRuntime(18083): at com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(Unknown Source) ERROR/AndroidRuntime(18083): at com.illposed.osc.utility.OSCPacketDispatcher.dispatchBundle(Unknown
错误/AndroidRuntime(18083): 在 android.view.ViewRoot.checkThread(ViewRoot.java:2811) 错误/AndroidRuntime(18083): 在 android.view.ViewRoot.requestLayout(ViewRoot.java:594) 错误/AndroidRuntime(18083) :在 android.view.View.requestLayout(View.java:8180) 错误/AndroidRuntime(18083):在 android.view.View.requestLayout(View.java:8180) 错误/AndroidRuntime(18083):在 android.view。 View.requestLayout(View.java:8180) 错误/AndroidRuntime(18083):在 android.view.View.setBackgroundDrawable(View.java:7535) 错误/AndroidRuntime(18083):在 android.view.View.setBackgroundColor(View. java:7429) 错误/AndroidRuntime(18083):在 com.taprik.Remote.RemoteMain$4.acceptMessage(RemoteMain.java:202) 错误/AndroidRuntime(18083):在 com.illposed.osc.utility.OSCPacketDispatcher.dispatchMessage(Unknown)来源)错误/AndroidRuntime(18083):在com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(未知来源)错误/AndroidRuntime(18083):在com.illposed.osc.utility.OSCPacketDispatcher.dispatchBundle(未知) Source) ERROR/AndroidRuntime(18083): at com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(Unknown Source) ERROR/AndroidRuntime(18083): at com.illposed.osc.OSCPortIn.run(Unknown Source) ERROR/AndroidRuntime(18083): at java.lang.Thread.run(Thread.java:1096) WARN/ActivityManager(6722): Force finishing activity com.taprik.Remote/.RemoteMain
来源)错误/AndroidRuntime(18083):在com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(未知来源)错误/AndroidRuntime(18083):在com.illposed.osc.OSCPortIn.run(未知来源)错误/AndroidRuntime( 18083): 在 java.lang.Thread.run(Thread.java:1096) WARN/ActivityManager(6722): 强制完成活动 com.taprik.Remote/.RemoteMain
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. android.view.ViewRoot$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
You can only alter UI elements on the UI thread: Note: Anything performed on the UI thread will block other UI actions.您只能在 UI 线程上更改 UI 元素:注意:在 UI 线程上执行的任何操作都会阻止其他 UI 操作。 Ergo, if you're cycling through some long list on the UI thread, the user cannot interact with the UI while it's happening.
因此,如果您在 UI 线程上循环浏览一些长列表,则用户无法在 UI 发生时与 UI 进行交互。
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
mScreen.setBackgroundColor(color);
}
});
Or:要么:
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
// Create the handler
mHandler = new Handler();
}
private void changeBgColor(final int color) {
mHandler.post(new Runnable() {
public void run() {
mScreen.setBackgroundColor(color);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.