0
  • 最佳答案
    A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.



    handler是用来发送消息和处理消息的。


    至于在什么线程发送消息,在什么线程处理消息就看你怎么创建。怎么调用。


    你所说的耗时操作需要handler是因为。耗时操作一般开子线程,异步数据。但是子线程不能直接更新UI,所以需要通过Handler发送消息,也就是把请求到的结果,封装成消息,放到队列里,然后由主线程的轮训器,去消费消息。也就是走取一HandlerMsg里了。






    1153952789488054272  评论     打赏       拉大锯
    • 这个问题很简单,会解不?


      没解决,不会解,还想尝试的话, 阅读一下这 篇文章吧。


      https://www.sunofbeach.net/a/1200314266318655488

      1153952789488054272  评论     打赏       拉大锯
      • 这里是空指针异常了吧.适配器为空了.


        你使用前得先创建呀.


        先问你一个问题,handler是用来干嘛的呢?

        1153952789488054272  评论     打赏       拉大锯
        • Neallu  回复 @拉大锯
          确实是空指针,我一开始adapter是写在耗时请求的最后新建的,然后直接添加数据,我现在单独拉出去,再开始新建adapter 就解决问题了,谢谢老师
          Neallu 2020-05-12 17:52   回复 1255348622380240896
        • Neallu  回复 @拉大锯
          我理解的handler是用来处理耗时操作的,比如倒计时当中的延时操作,1秒之后去message通知ui,做出改变。我记得老师讲recycleview 下拉刷新的时候,就用到了handler
          Neallu 2020-05-12 17:55   回复 1255348622380240896
        • Neallu  回复 @拉大锯
          我还不确定,如果我下拉,转圈,发出请求,再去更新recyclerview ,是不是需要用到handler
          Neallu 2020-05-12 17:56   回复 1255348622380240896
        • 拉大锯  回复 @Neallu
          我理解的handler是用来处理耗时操作的-->不对,错。赶紧忘记这个。这不对。
          拉大锯 2020-05-12 18:05   回复 1153952789488054272
      相关问题
      kissmyass · 安卓
      2019-12-11 04:41 713 2
      珂珂范范 · 安卓
      2019-12-19 00:59 558 2
      2020-01-31 00:31 693 2
      谁都不许动我的砖 · 安卓 / xml
      2020-02-05 19:16 1183 2
      谁都不许动我的砖 · 安卓
      2020-02-06 06:24 764 2
      珂珂范范 · 安卓模拟器
      2020-02-21 11:10 841 2
      珂珂范范 · 安卓
      2020-02-29 23:50 428 2
      mercenary · 安卓
      2020-03-22 02:34 355 2
      哦豁! · 安卓报错
      2020-03-22 23:17 682 4
      Pancica · 安卓
      2020-03-23 02:13 520 2
      Pancica · 安卓
      2020-03-30 08:22 452 2
      kuro · 安卓
      2020-04-04 00:17 415 4