为什么布局上的 setBackgroundColor 有时会使我的应用程序崩溃
我正在构建一个可以发送和接收 OSC 的应用程序。 我正在使用JavaOSC,它完全满足我的需求。
我在 OSC 消息中发送颜色值,并也接收它们。
我从基于此颜色选择器的颜色选择器发送颜色 http://code.google.com/p/color-picker-view/ 效果很好。
我的问题是:
- 当我在颜色选择器中选择颜色时,我设置布局的背景(颜色选择器所在的位置)视图:
私有ColorPickerView.OnColorChangedListener colorListener = new ColorPickerView.OnColorChangedListener(){
@Override
public void onColorChanged(int color) {
caller.sending("color", color);
mScreen.setBackgroundColor(color);
}
};
其中 mScreen 是 LinearLyout
mScreen = (LinearLayout) findViewById(R.id.myScreen);
它按预期工作。
- 当我收到带有颜色的 OSC 消息时,我也想更改背景,但它崩溃了。
我的听众是
OSCListener 监听器 = new 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;
}
您可以在下面找到崩溃报告。 函数 setBackgroundColor 是否需要在特定函数中才能正常工作(例如在 onClick 函数中)或者当它在屏幕上重绘某些内容时?
崩溃报告
08-05 15:18:15.035: WARN/dalvikvm(18083): threadid=7: 线程因未捕获的异常而退出 (group=0x4001d7d0) 错误/AndroidRuntime(18083):致命异常:线程 8 ERROR/AndroidRuntime(18083): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。 错误/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(来源未知) 错误/AndroidRuntime(18083):位于 com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(来源未知) 错误/AndroidRuntime(18083):位于 com.illspoke.osc.utility.OSCPacketDispatcher.dispatchBundle(来源未知) 错误/AndroidRuntime(18083):位于 com.illposed.osc.utility.OSCPacketDispatcher.dispatchPacket(来源未知) 错误/AndroidRuntime(18083):位于 com.illspoke.osc.OSCPortIn.run(来源未知) 错误/AndroidRuntime(18083):在java.lang.Thread.run(Thread.java:1096) WARN/ActivityManager(6722):强制完成活动 com.taprik.Remote/.RemoteMain
I'm building an application that can send and received OSC.
I'm using JavaOSC, it feets perfectly my needs.
I'm sending color value in OSC message, and receiving them also.
I send the color from a colorpicker based on this color picker
http://code.google.com/p/color-picker-view/
it works great.
My problem is :
- When I choose the color in the color picker i'm setting the background of the layout (where the color picker is) view with :
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 = (LinearLayout) findViewById(R.id.myScreen);
It works as expected.
- When I'm receiving an OSC message with color I'd like to change the background too, but it crashes.
My listener is
OSCListener listener = new 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 ?
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.
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。
您只能在 UI 线程上更改 UI 元素:
注意:在 UI 线程上执行的任何操作都会阻止其他 UI 操作。因此,如果您在 UI 线程上循环浏览某个长列表,则用户无法在交互过程中与 UI 进行交互。
或者:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
You can only alter UI elements on the UI thread:
Note: Anything performed on the UI thread will block other UI actions. Ergo, if you're cycling through some long list on the UI thread, the user cannot interact with the UI while it's happening.
Or: