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

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

在接优量汇的时候,这个方法写在哪里都报这个错误,可以这样修改下:

[GDTSDKConfig registerAppId:ULH_APPID];

改为:

   // 优量汇 异步加载

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        [GDTSDKConfig registerAppId:ULH_APPID];

    });

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值