简体   繁体   English

为什么布局上的 setBackgroundColor 有时会导致我的应用程序崩溃

[英]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 :我的问题是:

  • 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(){ 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.它按预期工作。

  • When I'm receiving an OSC message with color I'd like to change the background too, but it crashes.当我收到带有颜色的 OSC 消息时,我也想更改背景,但它崩溃了。

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.

相关问题 为什么更改布局时我的应用程序崩溃? - why my app crash when i change its layout? 为什么有时我的应用程序崩溃? - Why does my application crash sometimes? 为什么setBackgroundColor()仅对我的自定义主题失败? - Why is setBackgroundColor() failing only with my custom theme? 为什么设置 setBackgroundColor 在我的自定义 listView 中不起作用 - Why set setBackgroundColor is not working in my custom listView 打开我的应用程序时,应用有时崩溃 - App got crash sometimes when opening my app 为什么在布局中更改片段的位置会导致我的应用崩溃? - Why does changing the position of a fragment in my layout cause my app to crash? 为什么我的应用程序崩溃? - Why does my app crash? 为什么我的应用程序在某些设备上崩溃并更改布局,而在其他设备上即使尺寸不同也能完美运行 - Why my app crash on some devices and change layout while in other devices even with diffrent sizes it work perfectly onTouch中的android setBackgroundColor有时无法正常工作 - android setBackgroundColor in onTouch sometimes not working 为什么我的图形有时会使用相对布局居中? - Why is my graphic sometimes centering using a relative layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM