由于代码是这样写的,我认为跟线程有关系; 如果下发的是空的话,应该会crash住,并且报空指针错误,但是并不是,这儿的逻辑就是加载图片显示;debug了一下 是Loop的时候报出的,下发的并不是没有数据; 在Android4.0之后在主线程里面执行Http请求都会报NetworkOnMainThreadException的异常,是不应该再主线程中进行网络请求和UI操作的,这些操作往往耗费大量时间,会造成主线程的阻塞。 把runOnUiThread替换为 查看错误源码 可以看到 如果当前传入的view为空的话就会报错 这里讲到了Util.assertMainThread();方法,在之前的with和into里都没有见过这个方法。其实从名字我们已经可以看出一二: 首先判断是不是主线程,如果是的话,直接重写run方法; 如果不是呢? 使用Looper来处理,looper需要注意三个地方: (1).线程默认没有 Looper 调用looper.prepare 是判断是否绑定线程(,一个looper只能绑定一个线程),而调用looper.loop()是开始循环;当我们创建looper的时候,会创建一个消息队列,而这个消息队列里面存放的是什么,就是message; (2).当一个 Handler 被创建的时候,会被指向一个指定的 Looper(即,指向一个指定的线程) 如果我们希望消息持有一个任务(一个 Runnable),我们简单的将 Runnable 对象传递给 post() 方法就可以:
android.os.NetworkOnMainThreadException
解决事情应该优先解决一件事情,总是三心二意,最后塞翁失马
文章目录
问题分析
Drawable drawable = Drawable.createFromStream(new URL(response.getResult().get(0).getBgurl()).openStream(), null);
查阅资料
The exception that is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness. Also see StrictMode.*/
解决方案
new Thread(new Runnable){@override public void run()}.start();
java.lang.IllegalArgumentException: You must call this method on the main thread
//判断是不是主线程 Util.assertMainThread(); if (view == null) { throw new IllegalArgumentException("You must pass in a non null View"); } if (!isTransformationSet && view.getScaleType() != null) { switch (view.getScaleType()) { //center_crop的意思是将我们的图片尺寸按照imageview的尺寸进行适当缩放展示中心区域 case CENTER_CROP: applyCenterCrop(); break; case FIT_CENTER: case FIT_START: case FIT_END: applyFitCenter(); break; //$CASES-OMITTED$ default: // Do nothing. } } //关键句 上面的代码主要是图片处理 return into(glide.buildImageViewTarget(view, transcodeClass)); }
You must pass in a non null View
如果没有在主线程就会报错;
这时候我们需要把Glide.with(content).info().show();放到RunOnUiThread里面就可以了;知识要点: RunOnUiThread 和 new Thread(new Runnable()).start() 的区别
You must pass in a non null View;先说一下 RunonUiThread
* thread, then the action is executed immediately. If the current thread is * not the UI thread, the action is posted to the event queue of the UI thread. * * @param action the action to run on the UI thread */ public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action); } else { action.run(); } }
(2).你可创建一个 Looper 并将它绑定到一个线程
(3).每一个线程只能绑定一个 Looper
如果我们希望一个 Runnable 在指定的线程运行,我们只需要将它放到一个消息里,并将这个消息放到对应线程的 Looper 消息队列就可以了!
这时候就需要使用 handler来处理消息
(1).它负责向 Looper 的队列添加消息,当轮到消息执行时,它负责在 Looper 所在的线程中执行同一条消息。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算