Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新主线程外部的UI_Java_Android_Multithreading_User Interface - Fatal编程技术网

Java 更新主线程外部的UI

Java 更新主线程外部的UI,java,android,multithreading,user-interface,Java,Android,Multithreading,User Interface,我对android完全陌生,只想知道它是否能在主线程之外更新UI。仅从我下面列出的代码中,我就知道从这个代码中;这根本不可能。但是,问题是,我只想使用另一个线程更新UI。请先帮我谢谢 package com.example.app; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.vi

我对android完全陌生,只想知道它是否能在主线程之外更新UI。仅从我下面列出的代码中,我就知道从这个代码中;这根本不可能。但是,问题是,我只想使用另一个线程更新UI。请先帮我谢谢

 package com.example.app;

 import java.util.Random;
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
 import android.view.View;
  import android.widget.Button;
 import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
private Button b;
public ImageView I1;
 public ImageView I2;
 public ImageView I3;
public ImageView I4;
public TextView T;
public TextView s;
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 I1=new ImageView(this);
 I1=(ImageView) findViewById(R.id.imag1);
 I1.setVisibility(View.INVISIBLE);

 I2=new ImageView(this);
 I2=(ImageView) findViewById(R.id.imag2);
 I2.setVisibility(View.INVISIBLE);

 I3=new ImageView(this);
 I3=(ImageView) findViewById(R.id.imag3);
 I3.setVisibility(View.INVISIBLE);

 I4=new ImageView(this);
 I4=(ImageView) findViewById(R.id.imag4);
 I4.setVisibility(View.INVISIBLE);

 T=(TextView)findViewById(R.id.time);
 s=(TextView)findViewById(R.id.score);

Thread t=new Thread(new MyThread());
t.start();
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private class MyThread implements Runnable{
Random randomGenerator = new Random();
int n;
public void run(){

    while(true){
        n=randomGenerator.nextInt(8);

        if(n==1){
            I1.setVisibility(View.VISIBLE);

        }
        if(n==2){
            I2.setVisibility(View.VISIBLE);
        }

        if(n==3){
            I3.setVisibility(View.VISIBLE);
        }

        if(n==4){
            I4.setVisibility(View.VISIBLE);
        }
        try {
            Thread.currentThread().sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
             e.getStackTrace();
        }
    }

   }




   }
   }

只有主线程可以更新UI。如果线程想要更新UI,请使用activity.runOnUiThread()

然而,您的操作只是排队在UI线程中运行(如果您不是从UI线程运行),这意味着它可以稍后更新UI


只有主线程可以更新UI。如果线程想要更新UI,请使用activity.runOnUiThread()

然而,您的操作只是排队在UI线程中运行(如果您不是从UI线程运行),这意味着它可以稍后更新UI


使用activity.runOnUiThread

Acivity.runOnUiThread(new Runnable() {
    public void run() {
        //something here
    }
});

使用activity.runOnUiThread

Acivity.runOnUiThread(new Runnable() {
    public void run() {
        //something here
    }
});

您必须使用处理程序实例并使用post方法在UI线程的队列中添加Runnable。如果您在活动中,则可以使用runOnUiThread(这是一种“快捷方式”)执行相同的操作

您必须使用处理程序实例并使用post方法在UI线程的队列中添加Runnable。如果您在活动中,则可以使用runOnUiThread(这是一种“快捷方式”)执行相同的操作

您不能直接从非UI线程更新UI,但可以使用处理程序对象或异步任务对象与UI线程通信。使用AsyncTask最方便的方法是:

  • AsyncTask的.doInBackground()方法在非ui线程中运行
  • .onProgressUpdate()在UI线程中运行,因此可以更改视图
  • 您可以在doInBackground()中使用publishProgress()方法将数据传递给.onProgressUpdate
  • 很抱歉,如果方法名称中有一些错误。
    阅读了解详细信息。

    您不能直接从非UI线程更新UI,但可以使用Handler对象或AsyncTask对象与UI线程通信。使用AsyncTask最方便的方法是:

  • AsyncTask的.doInBackground()方法在非ui线程中运行
  • .onProgressUpdate()在UI线程中运行,因此可以更改视图
  • 您可以在doInBackground()中使用publishProgress()方法将数据传递给.onProgressUpdate
  • 很抱歉,如果方法名称中有一些错误。
    阅读了解详细信息。

    工作线程永远无法更新主线程,因为只有主线程可以呈现UI元素(TextView、EditText等),如果我们尝试更新,肯定会出现异常

    myAcitity.runOnUiThread(new Runnable() {
        public void run() {
            //here you can update your UI elements because this code is going to executed by main thread
        }
    });
    
    否则您可以使用视图类的post方法

    myView.post(new Runnable() {
            public void run() {
                //here you can update your UI elements because this code is going to executed by main thread
            }
        });
    

    工作线程永远无法更新主线程,因为只有主线程可以呈现UI元素(TextView、EditText等),如果我们尝试更新,肯定会出现异常

    myAcitity.runOnUiThread(new Runnable() {
        public void run() {
            //here you can update your UI elements because this code is going to executed by main thread
        }
    });
    
    否则您可以使用视图类的post方法

    myView.post(new Runnable() {
            public void run() {
                //here you can update your UI elements because this code is going to executed by main thread
            }
        });
    

    您应该阅读更多关于android和多线程的内容,因为您的代码存在设计缺陷。UI不应该由主线程之外的任何其他线程更新,如果您费心阅读文档,您就会知道这一点。然而,android为您提供了工具,使您能够从其他线程发送的订单中更新UI,但您必须阅读文档以了解如何更新。您能给我有关特定文档的链接吗?首先,您应该阅读更多关于android和多线程的信息,您的代码存在设计缺陷。UI不应该由主线程之外的任何其他线程更新,如果您费心阅读文档,您就会知道这一点。但是,android为您提供了工具,使您能够从其他线程发送的订单中更新UI,但您必须阅读文档以了解如何更新。您能给我有关该特定文档的链接吗?首先,这里:但是我可以使用sleep方法暂停执行吗。我的意思是,如果在RunOnUIThread()中使用sleep,主线程将被暂停,这将产生问题!这是使用UI的主要原则之一。阻止UI线程最短时间,在后台线程中执行逻辑,并使用相关准备好的数据安排UI更新。但是我可以使用sleep方法暂停执行吗。我的意思是,如果在RunOnUIThread()中使用sleep,主线程将被暂停,这将产生问题!这是使用UI的主要原则之一。在最短的时间内阻止UI线程,在后台线程中执行逻辑,只需使用相关准备好的数据安排UI更新。