Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso java.lang.IllegalStateException: Method call should not happen from the main thread

Tags:

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());
}

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?

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

like image 991
Brian Avatar asked Nov 28 '14 01:11

Brian


People also ask

What does Java lang IllegalStateException mean?

An IllegalStateException is a runtime exception in Java that is thrown to indicate that a method has been invoked at the wrong time. This exception is used to signal that a method is called at an illegal or inappropriate time.

Why we get Java lang IllegalStateException?

Class IllegalStateException. Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

Is IllegalStateException a checked exception?

IllegalStateException is the child class of RuntimeException and hence it is an unchecked exception.


1 Answers

None of above worked for me instead this

Handler uiHandler = new Handler(Looper.getMainLooper());
    uiHandler.post(new Runnable(){
        @Override
        public void run() {
            Picasso.with(Context)
                    .load(imageUrl)
                    .into(imageView);
        }
    });

Hope it may be useful for someone

like image 195
Nikhil Avatar answered Sep 27 '22 21:09

Nikhil