Picasso java.lang.IllegalStateException:方法调用不应该从主线程发生 [英] Picasso java.lang.IllegalStateException: Method call should not happen from the main thread

查看:1558
本文介绍了Picasso java.lang.IllegalStateException:方法调用不应该从主线程发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Picasso从网址

I am attempting to use Picasso to get three Bitmap images from a URL

public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab2);
  Drawable d1 = new BitmapDrawable(Picasso.with(Tab2.this).load(zestimateImg1).get());
}

我收到 FATAL EXCEPTION 使用此代码。我怀疑它与这个事实有关,这应该在 AsyncTask 中完成,但我无法让它工作。如果使用这是可以避免的,我想这样做而不使用 AsyncTask

I am getting FATAL EXCEPTION with this code. I suspect it has to do with the fact that this should be done within AsyncTask, but I can't get it to work. If using that is avoidable, I would like to do this without using AsyncTask.

我如何获得此代码在没有崩溃的情况下运行?

How can I get this code to run without crashing?

如果最好的方法是使用 AsyncTask ,那么该解决方案就可以了。

If the best way to do this is with AsyncTask, then that solution is ok.

推荐答案

您无法在主线程中发出同步请求。如果您不想使用AsyncThread,那么只需将Picasso与目标一起使用。

You cannot make synchronous requests in the main thread. If you dont want to use an AsyncThread then just use Picasso together with a Target.

Picasso.with(Tab2.this).load(zestimateImg1).into(new Target(...);

我建议你保存一个参考像你这样的目标:

I recommend you save a reference to your target like so:

Target mTarget =new Target (...); 

这是因为Picasso对它们使用弱引用,它们可能在进程完成之前被垃圾收集。

This is because Picasso uses weak references to them and they might be garbage collected before the process is finished.

这篇关于Picasso java.lang.IllegalStateException:方法调用不应该从主线程发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆