title: android async task in for loop
body: <p>i want to call async task from foor loop how i can do this?
   i have done this</p>
  
  <pre><code>for (int i = 0; i &lt; listimage.size(); i++) {
              GlobalVariable.set_URL(listimage.get(i) + "");
              System.out.println("from fav " + GlobalVariable.get_URL() + "");
              try {
                   obj = new JSONObject(GlobalVariable.get_URL());
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              GetContacts gc = new GetContacts();
              gc.execute();
  
          }
  </code></pre>
  
  <p>my logcat:</p>
  
  <pre><code>04-08 14:27:33.894: E/AndroidRuntime(2526): FATAL EXCEPTION: AsyncTask #5
  04-08 14:27:33.894: E/AndroidRuntime(2526): java.lang.RuntimeException: An error occured while executing doInBackground()
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.lang.Thread.run(Thread.java:856)
  04-08 14:27:33.894: E/AndroidRuntime(2526): Caused by: java.lang.NullPointerException
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at com.leeway.hdwallpaper.favourite_Activity$GetContacts.doInBackground(favourite_Activity.java:182)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at com.leeway.hdwallpaper.favourite_Activity$GetContacts.doInBackground(favourite_Activity.java:1)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  04-08 14:27:33.894: E/AndroidRuntime(2526):     ... 5 more
  04-08 14:27:34.305: E/WindowManager(2526): Activity com.leeway.hdwallpaper.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42642538 that was originally added here
  04-08 14:27:34.305: E/WindowManager(2526): android.view.WindowLeaked: Activity com.leeway.hdwallpaper.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42642538 that was originally added here
  04-08 14:27:34.305: E/WindowManager(2526):  at android.view.ViewRootImpl.&lt;init&gt;(ViewRootImpl.java:374)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:297)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:229)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:154)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.app.Dialog.show(Dialog.java:285)
  04-08 14:27:34.305: E/WindowManager(2526):  at com.leeway.hdwallpaper.favourite_Activity$GetContacts.onPreExecute(favourite_Activity.java:161)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.os.AsyncTask.execute(AsyncTask.java:534)
  04-08 14:27:34.305: E/WindowManager(2526):  at com.leeway.hdwallpaper.favourite_Activity.onCreateView(favourite_Activity.java:108)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.os.Handler.handleCallback(Handler.java:643)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.os.Handler.dispatchMessage(Handler.java:92)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.os.Looper.loop(Looper.java:137)
  04-08 14:27:34.305: E/WindowManager(2526):  at android.app.ActivityThread.main(ActivityThread.java:4803)
  04-08 14:27:34.305: E/WindowManager(2526):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-08 14:27:34.305: E/WindowManager(2526):  at java.lang.reflect.Method.invoke(Method.java:511)
  04-08 14:27:34.305: E/WindowManager(2526):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
  04-08 14:27:34.305: E/WindowManager(2526):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
  04-08 14:27:34.305: E/WindowManager(2526):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>This is my getcontact.doinbackground</p>
  
  <pre><code>protected Void doInBackground(Void... params) {
      sample = new ArrayList&lt;String&gt;();
  
      contactList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
      gv = (GridView) getActivity().findViewById(R.id.gridView1);
  
      ServiceHandler sh = new ServiceHandler();
  
  
      String jsonStr = sh.makeServiceCall(obj.toString(),
              ServiceHandler.GET);
      System.out.println("this is from do in.."
              + GlobalVariable.get_URL() + "");
      Log.d("Response: ", "&gt; " + jsonStr);
  
      if (jsonStr != null) {
          try {
              JSONObject jsonObj = new JSONObject(jsonStr);
  
              // Getting JSON Array node
              contacts = jsonObj.getJSONArray(TAG_WALLPAPER);
  
              // looping through All Contacts
              for (int i = 0; i &lt; contacts.length(); i++) {
                  JSONObject c = contacts.getJSONObject(i);
  
                  String id = c.getString(TAG_ID);
  
                  // Phone node is JSON Object
  
                  JSONObject phone = c.getJSONObject(TAG_IMAGE);
  
                  String thumb = phone.getString(TAG_THUMB);
                  String android = phone.getString(TAG_IMAGE_ANDROID);
  
                  // img_url.add(thumb);
                  // grdadpter ga = new grdadpter(getActivity(), sample);
                  // gv.setAdapter(ga);
                  // String office = phone.getString(TAG_IMAGE_OFFICE);
  
                  // tmp hashmap for single contact
                  HashMap&lt;String, String&gt; contact = new HashMap&lt;String, String&gt;();
  
                  // adding each child node to HashMap key =&gt; value
                  contact.put(TAG_ID, id);
                  img_ID.add(id);
                  // adding contact to contact list
                  contactList.add(contact);
                  // sample.add(thumb);
                  publishProgress(thumb);
                  android_img.add(android);
                  System.out.println("this is from do in ng.."
                          + sample.size());
  
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
      } else {
          Log.e("ServiceHandler", "Couldn't get any data from the url");
      }
  
      return null;
  
  }
  </code></pre>