title: Universal Image Loader use from asynctask
body: <p>I want to use UIL from asynctask.</p>
  
  <pre><code>    import com.nostra13.universalimageloader.core.DisplayImageOptions;
      import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  
      public class SingleAppDetail extends Activity {
          Button btnLogout;
          Button btShowProgress;
          // Progress Dialog
          private ProgressDialog pDialog;
          JSONparser jParser = new JSONparser();
          // Progress dialog type (0 - for Horizontal progress bar)
          public static final int progress_bar_type = 0;
          private static String file_url = "http://site.com/as.apk";
      JSONArray detalii = null;
      TextView app_Detail_title;
      Button app_price_button;
      TextView app_Description;
      ImageView app_Picture;
      View loadingPanelViev;
      View appDetailHeader;
      RatingBar appDetailRating;
      RatingBar rating_1;
      RatingBar rating_2;
      RatingBar rating_3;
      RatingBar rating_4;
      RatingBar rating_5;
      RatingBar rating_ff;
      TextView rating_ff_text;
      TextView rating_1_text;
      TextView rating_2_text;
      TextView rating_3_text;
      TextView rating_4_text;
      TextView rating_5_text;
      String[] imageUrls;
      ImageSwitcher appGallery;
      DisplayImageOptions options;
      com.nostra13.universalimageloader.core.ImageLoader ImageLoader;
      Gallery app_gallery;
      ArrayList&lt;String&gt; urlse = new ArrayList&lt;String&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.app_detail);
          app_Detail_title = (TextView) findViewById(R.id.app_detail_title);
          app_price_button = (Button) findViewById(R.id.app_detail_price);
          app_Description = (TextView) findViewById(R.id.description_holder);
          app_Picture = (ImageView) findViewById(R.id.singleAppDetailImage);
          loadingPanelViev = (View) findViewById(R.id.loadingPanelAppDetailHeader);
          appDetailHeader = (View) findViewById(R.id.app_detail_header_content);
          appDetailRating = (RatingBar) findViewById(R.id.app_detail_rating);
          rating_1 = (RatingBar) findViewById(R.id.rating_1);
          rating_2 = (RatingBar) findViewById(R.id.rating_2);
          rating_3 = (RatingBar) findViewById(R.id.rating_3);
          rating_4 = (RatingBar) findViewById(R.id.rating_4);
          rating_5 = (RatingBar) findViewById(R.id.rating_5);
          rating_ff = (RatingBar) findViewById(R.id.rating_ff);
          rating_ff_text = (TextView) findViewById(R.id.rating_ff_text);
          rating_1_text = (TextView) findViewById(R.id.rating_1_text);
          rating_2_text = (TextView) findViewById(R.id.rating_2_text);
          rating_3_text = (TextView) findViewById(R.id.rating_3_text);
          rating_4_text = (TextView) findViewById(R.id.rating_4_text);
          rating_5_text = (TextView) findViewById(R.id.rating_5_text);
          // appGallery=(ImageSwitcher)findViewById(R.id.app_detail_gallery);
          app_gallery = (Gallery) findViewById(R.id.app_gallery);
          DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                  .cacheInMemory().build();
  
          ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                  getApplicationContext()).defaultDisplayImageOptions(
                  defaultOptions).build();
          ImageLoader.getInstance().init(config);
  
          new loadAppMainDetails(getApplicationContext()).execute();
          new loadAppMainRating(getApplicationContext()).execute();
          new loadAppMainImages(getApplicationContext()).execute();
          Button download = (Button) findViewById(R.id.app_detail_price);
          download.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                  new DownloadFileFromURL().execute(file_url);
              }
          });
  
      }
  
      /**
       * Showing Dialog
       * */
      @Override
      protected Dialog onCreateDialog(int id) {
          //this is ok
      }
  
      /**
       * Background Async Task to download file
       * */
  
      class DownloadFileFromURL extends AsyncTask&lt;String, String, String&gt; {
  //this is ok
  }
  
  
  
      class loadAppMainDetails extends
              AsyncTask&lt;String, String, HashMap&lt;String, String&gt;&gt; {
  //this is ok
  }
  
      class loadAppMainRating extends AsyncTask&lt;String, String, String&gt; { 
  this is ok;
  }
  // here starts the problem
      class loadAppMainImages extends AsyncTask&lt;String, String, String&gt; {
          Context mContext;
          JSONArray appDetailPictures;
          String image_path;
          public loadAppMainImages(Context context) {
              super();
              this.mContext = context;
          }
  
          @Override
          protected String doInBackground(String... params) {
              Intent is = getIntent();
              String app_id = is.getStringExtra("element_id");
              List&lt;NameValuePair&gt; parameters = new ArrayList&lt;NameValuePair&gt;();
              parameters.add(new BasicNameValuePair("app_id", app_id));
              JSONObject json = jParser.makeHttpRequest(
                      "http://himbimarket.com/masterApp/get_app_pictures.php",
                      "POST", parameters);
              try {
                  int success = json.getInt("success");
                  if (success == 1) {
                      appDetailPictures = json.getJSONArray("images");
                      for (int i = 0; i &lt; appDetailPictures.length(); i++) {
                          JSONObject c = appDetailPictures.getJSONObject(i);
                          // Storing each json item in variable
                          String picname = c.getString("picture");
                          image_path = "http://www.himbimarket.com/masterApp/"
                                  .concat(picname);
                          urlse.add(image_path);
                      }
  
                  }
              } catch (Exception e) {
  
                  // TODO: handle exception
              }
  
              return null;
          }
  
          @Override
          protected void onPostExecute(String result) {
  
              Toast.makeText(getApplicationContext(), urlse.get(2),
                      Toast.LENGTH_LONG).show();
  
               app_gallery.setAdapter(new ImageGalleryAdapter());
  
          }
  
  
  
      }
  
      public class ImageGalleryAdapter extends BaseAdapter {
  
          @Override
          public int getCount() {
              return urlse.size();
          }
  
          @Override
          public Object getItem(int position) {
              return position;
          }
  
          @Override
          public long getItemId(int position) {
              return position;
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              ImageView imageView = (ImageView) convertView;
              if (imageView == null) {
                  imageView = (ImageView) getLayoutInflater().inflate(
                          R.layout.item_gallery_image, parent, false);
              }
  
              ImageLoader.displayImage(urlse.get(position), imageView);
              return imageView;
          }
      }
  
      }
  </code></pre>
  
  <p>I don't figure out why is not working.</p>
  
  <p>The error code is bellow:</p>
  
  <pre><code>        04-19 06:52:19.635: E/AndroidRuntime(1593): FATAL EXCEPTION: main
      04-19 06:52:19.635: E/AndroidRuntime(1593): java.lang.NullPointerException
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at com.himbi.market.SingleAppDetail$ImageGalleryAdapter.getView(SingleAppDetail.java:471)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.ScrollView.onMeasure(ScrollView.java:321)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.View.measure(View.java:15513)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.Choreographer.doFrame(Choreographer.java:532)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
      04-19 06:52:19.635: E/AndroidRuntime(1593):     at android.os.Handler.handleCallback(Handler.java:725)
  </code></pre>
  
  <p>It hass a problem in adapter when he tries to load imageview.</p>
  
  <p>This is the app_detail.xml</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/app_detail_back_image" &gt;
  
          &lt;LinearLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/app_detail_back_image"
              android:orientation="vertical" &gt;
  
              &lt;RelativeLayout
                  android:id="@+id/loadingPanelAppDetailHeader"
                  style="@style/GenericProgressBackground"
                  android:layout_height="fill_parent" &gt;
  
                  &lt;ProgressBar style="@style/GenericProgressIndicator" /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/app_detail_header"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:divider="@drawable/divider"
                  android:paddingBottom="5dp"
                  android:paddingLeft="5dp"
                  android:paddingTop="5dp" &gt;
  
                  &lt;ImageView
                      android:id="@+id/singleAppDetailImage"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/nopic" /&gt;
  
                  &lt;RelativeLayout
                      android:id="@+id/app_detail_header_content"
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent" &gt;
  
                      &lt;includes.TextResize
                          android:id="@+id/app_detail_title"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:layout_alignParentTop="true"
                          android:maxLines="2"
                          android:text="First Application"
                          android:textColor="@color/himbi_apps_color"
                          android:textSize="22sp" /&gt;
  
                      &lt;Button
                          android:id="@+id/app_detail_price"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:layout_alignParentBottom="true"
                          android:layout_alignParentRight="true"
                          android:background="@drawable/app_detail_price_back"
                          android:text="Button" /&gt;
  
                      &lt;TextView
                          android:id="@+id/app_detail_developer"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_alignParentLeft="true"
                          android:layout_below="@+id/app_detail_title"
                          android:text="TextView"
                          android:textSize="14sp" /&gt;
                  &lt;/RelativeLayout&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="150dp" &gt;
  
                  &lt;Gallery
                      android:id="@+id/app_gallery"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_vertical"
                      android:spacing="1dip" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" &gt;
  
                  &lt;RelativeLayout
                      android:layout_width="fill_parent"
                      android:layout_height="match_parent" &gt;
  
                      &lt;RatingBar
                          android:id="@+id/app_detail_rating"
                          style="?android:attr/ratingBarStyleSmall"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content" /&gt;
  
                      &lt;TextView
                          android:id="@+id/app_date"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_alignParentRight="true"
                          android:layout_alignParentTop="true"
                          android:text="Small Tijkjext"
                          android:textAppearance="?android:attr/textAppearanceSmall" /&gt;
                  &lt;/RelativeLayout&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" &gt;
  
                  &lt;RelativeLayout
                      android:layout_width="fill_parent"
                      android:layout_height="match_parent" &gt;
  
                      &lt;TextView
                          android:id="@+id/description_holder"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_alignParentLeft="true"
                          android:layout_alignParentTop="true"
                          android:text="Description Holder" /&gt;
                  &lt;/RelativeLayout&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" &gt;
  
                  &lt;RelativeLayout
                      android:layout_width="fill_parent"
                      android:layout_height="match_parent"
                      android:layout_marginLeft="20dp" &gt;
  
                      &lt;TextView
                          android:id="@+id/textView2"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_alignParentLeft="true"
                          android:layout_alignParentTop="true"
                          android:text="@string/rating"
                          android:textAppearance="?android:attr/textAppearanceMedium"
                          android:textColor="@color/himbi_color" /&gt;
  
  
                      &lt;RelativeLayout
                          android:id="@+id/relativeLayout1"
                          android:layout_width="100dp"
                          android:layout_height="100dp"
                          android:layout_margin="25dp"
                          android:layout_toRightOf="@+id/relativeLayout2" &gt;
  
                          &lt;TextView
                              android:id="@+id/rating_ff_text"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_alignRight="@+id/textView8"
                              android:layout_centerVertical="true"
                              android:gravity="center_horizontal"
                              android:text="4.5"
                              android:textAppearance="?android:attr/textAppearanceLarge"
                              android:textColor="@color/himbi_apps_color" /&gt;
  
                          &lt;RatingBar
                              android:id="@+id/rating_ff"
                              style="?android:attr/ratingBarStyleSmall"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_below="@+id/rating_ff_text"
                              android:layout_centerHorizontal="true"
                              android:padding="2dp" /&gt;
  
                          &lt;TextView
                              android:id="@+id/textView8"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_alignParentLeft="true"
                              android:layout_alignParentTop="true"
                              android:background="#a0a0a0"
                              android:gravity="center_horizontal"
                              android:text="Rating"
                              android:textAppearance="?android:attr/textAppearanceMedium"
                              android:textColor="@color/header" /&gt;
                      &lt;/RelativeLayout&gt;
                  &lt;/RelativeLayout&gt;
              &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
  
      &lt;/ScrollView&gt;
  </code></pre>
  
  <p>And the xml for inflate:</p>
  
  <pre><code>      &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;Gallery xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/gallery"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical"
          android:spacing="1dip" /&gt;
  </code></pre>
  
  <p>Android version is 4.0.1 and UIL is the latest.</p>
  
  <p>Thank you</p>