This method should not be called on the main thread as it may lead to UI unresponsiveness报错

ios安装包用webview加载H5页面报:

This method should not be called on the main thread as it may lead to
UI unresponsiveness

这个错误

原因:
点击的是ios的头部的状态栏,是安全区域,才会报这个错,导致点击触发的事件可能时灵时不灵。
解决:
操作按钮离开安全区,用苹果自带的constant(safe-area-inset-top)和env(safe-area-inset-top);这两个值来有效规避在安全区的操作。或者加固定padding之类的也可以。
具体使用参考文章:
https://blog.csdn.net/zuoyiran520081/article/details/108366329

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们在主线程中访问一些耗时的操作,比如网络请求、文件读写等,会导致主线程被阻塞,从而造成界面卡顿,用户体验不好。为了避免这种情况,我们通常会将耗时操作放到子线程中进行,然后再将结果返回到主线程更新UI。如果在主线程中调用了一些不允许在主线程中执行的方法,就会抛出“This method can not be called from the main application thread”异常。 下面是一个常见的例子,当在主线程中进行网络请求时,就会抛出该异常: ```java public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //创建一个TextView控件 textView = new TextView(this); textView.setText("Hello, UI Thread!"); //将TextView控件添加到当前Activity的界面中 setContentView(textView); //在主线程中进行网络请求 OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://www.baidu.com") .build(); Response response = client.newCall(request).execute(); //抛出异常 String result = response.body().string(); //更新UI textView.setText(result); } } ``` 在这个例子中,我们在主线程中进行了一个网络请求,并且在请求完成后试图更新UI。由于网络请求是一个耗时操作,如果直接在主线程中执行,就会导致主线程被阻塞,从而造成界面卡顿。因此,OkHttpClient会抛出“This method can not be called from the main application thread”异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值