Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
使用线程更新Android用户界面_Android_User Interface - Fatal编程技术网

使用线程更新Android用户界面

使用线程更新Android用户界面,android,user-interface,Android,User Interface,我正在写一个Android聊天应用程序。我正在侦听连接并接收数据,我可以在Log.d中看到它,但每当我尝试更新UI时,应用程序就会崩溃 代码段: 要更新我使用的UI,请执行以下操作: public static void updateUI(Bubble b, View itemView) { TextView txt_display_name = (TextView) itemView .findViewById(R.id.display_name);

我正在写一个Android聊天应用程序。我正在侦听连接并接收数据,我可以在
Log.d
中看到它,但每当我尝试更新UI时,应用程序就会崩溃

代码段:

要更新我使用的UI,请执行以下操作:

public static void updateUI(Bubble b, View itemView) {

    TextView txt_display_name = (TextView) itemView
            .findViewById(R.id.display_name);
    txt_display_name.setText(b.getDisplay_name());
    TextView txt_chat_body = (TextView) itemView
            .findViewById(R.id.chat_body);
    txt_chat_body.setText(b.getChat_body());
    TextView txt_creation_date = (TextView) itemView
            .findViewById(R.id.creation_date);
    txt_creation_date.setText(b.getCreation_time());
}

应用程序不断崩溃。

您无法从非UI线程更改UI元素。尝试使用


您不能从后台线程触摸UI线程中的任何内容,为此,请使用
处理程序
,初始化后台
线程
并向其传递
处理程序
对象。当数据到达时,使用
处理程序
向UI发送消息。在UI中,当来自后台
线程的消息出现时,只需更新
视图

示例代码段:

在后台线程中:

在UI线程中:

然后将
处理程序
传递到后台
线程

或者直接使用,这对IMHO更有用。

使用方法从工作线程更新UI,这里有很好的解释


注意:-如果您使用的是
progressbar.setProgress(progress)
您可以直接从worker
线程进行更新,因为
progressbar
在上一条语句中使用
Handler

内部调用
setProgress()
方法,如何将处理程序传递到后台线程???UI线程在更新过程中仍然冻结。@laph这就像程序员在@Franco-1中省略了关于“将处理程序传递到后台线程”的部分,这似乎是最重要的部分。如果我不能传递处理程序,它将无法工作。并非所有情况下都能将处理程序转移到后台线程。显然,当你像我一样使用Evernote的android工作时。而且,49次投票的答案对我来说不起作用。@DBX12处理程序只是一个变量,您可以将其作为类的一个成员来访问它。为了澄清这一点,您仍然无法在AsyncTask的
doInBackground
方法中更新UI,您需要在
pre
post
执行方法中进行更新。@Sheharyar非常正确,在这里面临同样的问题。无法在doInBackground方法中更新ui。回答上面的两条注释-您可以在
doInBackground()
中完成工作,并在
OnPostExecute()
中更新ui。在问题的上下文中-将从
doInBackground()
中检索服务器的数据,并将检索到的数据设置为
TextView
中的
OnPostExecute()
。该类在API级别30中已被弃用。请改用标准的java.util.concurrent或Kotlin并发实用程序。如果您正在线程上执行一些繁重的任务,并且希望更新某些UI元素(Edittext、Textview等),则必须使用runOnUIThread方法编写更新UI代码。。如下所示:homeActivity.runOnUiThread(new Runnable(){@Override public void run(){txtview.setText(“使用值更新”);}});使用runOnUiThread只能用于非常短的任务。如果任务耗时较长,则用户将体验冻结应用程序。
public static void updateUI(Bubble b, View itemView) {

    TextView txt_display_name = (TextView) itemView
            .findViewById(R.id.display_name);
    txt_display_name.setText(b.getDisplay_name());
    TextView txt_chat_body = (TextView) itemView
            .findViewById(R.id.chat_body);
    txt_chat_body.setText(b.getChat_body());
    TextView txt_creation_date = (TextView) itemView
            .findViewById(R.id.creation_date);
    txt_creation_date.setText(b.getCreation_time());
}
runOnUiThread(new Runnable(){
    @Override
    public void run(){
        // change UI elements here
    }
});
if(dataArrives){
    Message msg = handler.obtainMessage();
    msg.what = UPDATE_IMAGE;
    msg.obj = bitmap;
    msg.arg1 = index;
    handler.sendMessage(msg);
}
final Handler handler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
    if(msg.what==UPDATE_IMAGE){
      images.get(msg.arg1).setImageBitmap((Bitmap) msg.obj);
    }
    super.handleMessage(msg);
  }
};