当前位置:网站首页>Handler asynchronous message processing
Handler asynchronous message processing
2022-06-25 10:22:00 【Peach doesn't come out】
to update UI Must operate in the main thread , If it is updated in a non main thread UI Will report a mistake :
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
reason : Updated on non main thread UI, as follows :
Solution :handler Asynchronous thread
package com.tzbc.databindtest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "tzbc";
private static final int MSG_UPDATE = 415;
private Button button;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if(msg.what == MSG_UPDATE){
button.setText("new Thread");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btUpdate);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(MSG_UPDATE);
}
});
}
}
When other contents need to be carried along , Make a statement message:
package com.tzbc.databindtest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "tzbc";
private static final int MSG_UPDATE = 415;
private Button button;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == MSG_UPDATE) {
button.setText(msg.obj.toString());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btUpdate);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String updateStr = "2020/04/15";
Message message = Message.obtain();
message.what = MSG_UPDATE;
message.obj = updateStr;
handler.sendMessage(message);
}
});
}
}
边栏推荐
- Mqtt beginner level chapter
- Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决
- 依赖属性、依赖附加属性以及类型转换
- Houdini图文笔记:Your driver settings have been set to force 4x Antialiasing in OpenGL applications问题的解决
- Shardingsphere proxy 4.1 Sous - base de données sous - table
- [paper reading | depth] role based network embedding via structural features reconstruction with degree regulated
- The way that flutter makes the keyboard disappear (forwarding from the dependent window)
- Ruiji takeout project (II)
- Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
- Your driver settings have been set to force 4x antialiasing in OpenGL applications
猜你喜欢
How to make small programs on wechat? How to make small programs on wechat
How to build a wechat applet? How to open an applet?
Houdini graphic notes: could not create OpenCL device of type (houdini_ocl_devicetype) problem solving
The path of Architects
[paper reading | depth] role based network embedding via structural features reconstruction with degree regulated
Free platform for wechat applet making, steps for wechat applet making
Flask blog practice - realize the latest articles and search in the sidebar
ShardingSphere-Proxy 4.1 分库分表
Jetpack compose layout (III) - custom layout
Mongodb's principle, basic use, clustering and partitioned clustering
随机推荐
Force buckle -104 Maximum depth of binary tree
Flask博客实战 - 实现侧边栏最新文章及搜索
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
Modbus协议与SerialPort端口读写
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
Android database security: after the user exits, the transaction rollback log still stores relevant data information
Kotlin common standard functions
The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
我的作文题目是——《我的区长父亲》
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
Summary of considerations for native applet development
WPF 绑定表达式和绑定数据源(一)
Requirements and precautions for applying for multi domain SSL certificate
单片机进阶---PCB开发之照葫芦画瓢(二)
【论文阅读|深读】DRNE:Deep Recursive Network Embedding with Regular Equivalence
This is enough for request & response
On binary tree
DDS learning notes of opendds
在Microsoft Exchange Server 2007中安装SSL证书的教程
MySQL create given statement