Java Android “只有创建视图层次结构的原始线程才能触及其视图。” for循环出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38743402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:34:42  来源:igfitidea点击:

Android “Only the original thread that created a view hierarchy can touch its views.” error in for loop

javaandroidmultithreading

提问by Ariel

I am trying to run the for loop in every half second.The loop is changing something in the view every time it is called,but the changes are made by another class i.e Speedometer.

我试图每半秒运行一次 for 循环。每次调用时,循环都会更改视图中的某些内容,但这些更改是由另一个类(即速度计)进行的。

Thread thread=new Thread(){
        @Override
        public void run() {
            float i;
            try {

                for ( i = 0; i <= 100; i++) {
                            Speedometer1.onSpeedChanged(Speedometer1.getCurrentSpeed(i) + 8);
                            Speedometer2.onSpeedChanged(Speedometer2.getCurrentSpeed(i) + 8);
                            Speedometer3.onSpeedChanged(Speedometer3.getCurrentSpeed(i) + 8);
                            Speedometer4.onSpeedChanged(Speedometer4.getCurrentSpeed(i) + 8);
                            Speedometer5.onSpeedChanged(Speedometer5.getCurrentSpeed(i) + 8);
                            Speedometer6.onSpeedChanged(Speedometer6.getCurrentSpeed(i) + 8);


                    sleep(500);
                }
            }
            catch (InterruptedException e)
            {e.printStackTrace();}
        }
    };thread.start();

采纳答案by Miguel Benitez

That's because any view can be only modify in the main thread or ui Thread. Try running onSpeedChanged()using runOnUiThread(). Like this:

那是因为任何视图都只能在主线程或 ui 线程中进行修改。尝试onSpeedChanged()使用runOnUiThread(). 像这样:

    Thread thread=new Thread(){
    @Override
    public void run() {
        float i;
        try {

            for ( i = 0; i <= 100; i++) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Speedometer1.onSpeedChanged(Speedometer1.getCurrentSpeed(i) + 8);
                        Speedometer2.onSpeedChanged(Speedometer2.getCurrentSpeed(i) + 8);
                        Speedometer3.onSpeedChanged(Speedometer3.getCurrentSpeed(i) + 8);
                        Speedometer4.onSpeedChanged(Speedometer4.getCurrentSpeed(i) + 8);
                        Speedometer5.onSpeedChanged(Speedometer5.getCurrentSpeed(i) + 8);
                        Speedometer6.onSpeedChanged(Speedometer6.getCurrentSpeed(i) + 8)

                    }

                });


                sleep(500);
            }
        }
        catch (InterruptedException e)
        {e.printStackTrace();}
    }
};thread.start();

回答by Mujammil Ahamed

You should update the UI Components in runOnUIThread.Sample code is

您应该更新runOnUIThread.Sample 代码中的 UI 组件是

runOnUiThread(new Runnable() {
     @Override
     public void run() {

       //stuff that updates ui

    }
});

回答by Larry Schiefer

You cannot modify Viewobjects from outside the main thread. Even though your Speedometerclass is making the changes, it is running in the secondary thread you have created.

您不能View从主线程外部修改对象。即使您的Speedometer类进行了更改,它也在您创建的辅助线程中运行。

You could create a Runnableand post with deejay to a Handlercreated by the main thread, or use other similar techniques to accomplish the same.

您可以创建 aRunnable并使用 deejay 发布到Handler主线程创建的a ,或使用其他类似技术来完成相同的操作。

回答by xdevs23

You should update your UI on the UI thread.

您应该在 UI 线程上更新您的 UI。

For that, create a handler in your onCreate() method:

为此,在您的 onCreate() 方法中创建一个处理程序:

private Handler mHandler;

@Override
public void onCreate() {
    mHandler = new Handler();
    // ...
}

Then call the handler in your separate thread to execute it on the main thread:

然后在单独的线程中调用处理程序以在主线程上执行它:

mHandler.post(new Runnable() {
    @Override
    public void run() {
        // Update your UI
    }
});