title: Use Picasso inside Asynctask inside Fragment java.lang.IllegalStateException
body: <p>Good day guys, I have a problem using picasso to fetch image from my server to set as image of my ImageView, that Picasso was inside the class that extends Asynctask and that class was called from my class that extends Fragment, I think I had a same Problem about this guy <a href="https://stackoverflow.com/questions/29080197/loading-image-using-picasso-inside-asynctask">Loading Image using picasso inside AsyncTask</a> but even i use the method in the answer there It gives me a same error again and again, please help me guys, i'm stuck in here
  :(</p>
  
  <p>This is my class that extends Asynctask <br/> 
  PostParser.java</p>
  
  <pre><code>public class PostsParser extends AsyncTask&lt;Void,Void,Integer&gt; {
  
  Context c;
  ProgressBar pb;
  String jsonData;
  LinearLayout linear;
  
  public PostsParser(Context c, ProgressBar pb, String jsonData, LinearLayout linear){
      this.c = c;
      this.pb = pb;
      this.jsonData = jsonData;
      this.linear = linear;
  }
  
  @Override
  protected Integer doInBackground(Void... params) {
      return this.parseTag();
  }
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
      pb.setVisibility(View.VISIBLE);
  }
  
  @Override
  protected void onPostExecute(Integer result) {
      super.onPostExecute(result);
  
      if(result == 0){
          Toast noData = Toast.makeText(c, "No data found!", Toast.LENGTH_LONG);
          noData.setGravity(Gravity.CENTER, 0,0);
          noData.show();
      } else {
          if(pb.getVisibility()==View.VISIBLE){
              pb.setVisibility(View.GONE);
          }
      }
  }
  
  private int parseTag(){
      try{
          JSONArray ja = new JSONArray(jsonData);
          JSONObject jo = null;
  
          for(int i = 0;i&lt;ja.length();i++){
              jo=ja.getJSONObject(i);
  
              int id = jo.getInt("id");
              final String name = jo.getString("name");
              final String postsText = jo.getString("posts_text");
              final String imageUrl = jo.getString("image_url");
  
              linear.setOrientation(LinearLayout.VERTICAL);
  
              TextView pText = new TextView(c);
              pText.setText(postsText);
              pText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                      LinearLayout.LayoutParams.WRAP_CONTENT));
              linear.addView(pText);
  
              ImageView pImageView = new ImageView(c);
              pImageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
              pImageView.setPadding(0,0,0,16);
              loadImageInBackground(pImageView, imageUrl);
  
              linear.addView(pImageView);
  
          }
          return 1;
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      return 0;
  }
  
  public void loadImageInBackground(final ImageView iv, String imageUrl){
      Target target = new Target(){
  
          @Override
          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
              iv.setImageBitmap(bitmap);
          }
  
          @Override
          public void onBitmapFailed(Drawable errorDrawable) {
              pb.setVisibility(View.GONE);
              Toast.makeText(c, "Can't load image", Toast.LENGTH_LONG).show();
          }
  
          @Override
          public void onPrepareLoad(Drawable placeHolderDrawable) {
              pb.setVisibility(View.VISIBLE);
          }
      };
      Picasso.with(c).load(imageUrl).into(target); //line 127
  }  }
  </code></pre>
  
  <p>And this is my java class that extends Fragment<br/>
  TabPosts.java</p>
  
  <pre><code>  public class TabPosts extends Fragment {
  
      LinearLayout linearLayout;
      ProgressBar pb_posts;
      View rootView;
      String companyName;
  
      final static String URL = "http://192.168.8.101/servicefinder/DbPostsConnection.php";
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.company_posts, container, false);
  
          companyName = getArguments().getString("CompanyName");
  
          return rootView;
      }
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          linearLayout = (LinearLayout)rootView.findViewById(R.id.linear);
          pb_posts = (ProgressBar)rootView.findViewById(R.id.pb_posts);
  
          new DownloaderPosts(getActivity(),URL,pb_posts,companyName,linearLayout).execute();
  
      }
  }
  </code></pre>
  
  <p>I called the PostParser.java inside this class thats also extends Asynctask but I think this class didn't do anything wrong because i always using it. I JUST feel like I have to post it here :)<br/>
  DownloaderPosts.java</p>
  
  <pre><code>@Override
  protected void onPreExecute() {
      super.onPreExecute();
      if(pb.getVisibility() == View.GONE){
          pb.setVisibility(View.VISIBLE);
      }
  }
  
  @Override
  protected void onPostExecute(String jsonData) {
      super.onPostExecute(jsonData);
  
      pb.setVisibility(View.GONE);
  
      if(jsonData == null){
          Toast.makeText(c, "Network Error", Toast.LENGTH_LONG).show();
      } else {
          PostsParser postsParser = new PostsParser(c, pb, jsonData, linear);
          postsParser.execute();
      }
  }
  </code></pre>
  
  <p>And this is my error.</p>
  
  <pre><code>FATAL EXCEPTION: AsyncTask #1
                                                                        Process: com.gawapa.servicefinder, PID: 12466
                                                                        java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                            at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                                            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                            at java.lang.Thread.run(Thread.java:841)
                                                                         Caused by: java.lang.IllegalStateException: Method call should happen from the main thread.
                                                                            at com.squareup.picasso.Utils.checkMain(Utils.java:136)
                                                                            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:496)
                                                                            at com.gawapa.servicefinder.mMySQL.PostsParser.loadImageInBackground(PostsParser.java:127)
                                                                            at com.gawapa.servicefinder.mMySQL.PostsParser.parseTag(PostsParser.java:91)
                                                                            at com.gawapa.servicefinder.mMySQL.PostsParser.doInBackground(PostsParser.java:43)
                                                                            at com.gawapa.servicefinder.mMySQL.PostsParser.doInBackground(PostsParser.java:27)
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                            at java.lang.Thread.run(Thread.java:841) 
  </code></pre>