title: Android: How to have list of checkbox with image in AlertDialog
body: <p><img src="https://i.stack.imgur.com/Zpvtm.png" alt="enter image description here"></p>
  
  <p>I need my AlertDialog like the above image but <strong>WITH Checkbox</strong></p>
  
  <p>I had checked this <a href="https://stackoverflow.com/questions/3211927/android-image-inside-single-select/3281239#3281239">Post</a> and i had created the following codes</p>
  
  <p><strong>animal_text.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout android:padding="10dp" android:layout_height="fill_parent"    android:layout_width="fill_parent" android:orientation="horizontal"     android:id="@+id/layout_root" xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
  &lt;CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/miastorow_text"
      android:layout_width="fill_parent"
      android:layout_height="?android:attr/listPreferredItemHeight"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:gravity="center_vertical"
      android:checkMark="?android:attr/listChoiceIndicatorMultiple"
      android:paddingLeft="6dip"
      android:paddingRight="6dip"
      android:textColor="#000"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" &gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>ImageActivity.java</strong></p>
  
  <pre><code>package com.imagetest;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.DialogInterface.OnClickListener;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.CheckedTextView;
  import android.widget.ListAdapter;
  
  public class ImageActivity extends Activity {
      /** Called when the activity is first created. */
      private static final String TAG = "HelloWorld";
      Animal[] _animals;
      Button btnStart;
      public static int count =0;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          System.out.println("on create");
          setContentView(R.layout.main); 
          loadAnimals();
          btnStart = (Button)findViewById(R.id.button1);
          final Button button = (Button) findViewById(R.id.button1);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                   System.out.println("##########");
                   handlePush(v);
              }
          });
          //btnStart.setOnClickListener((android.view.View.OnClickListener) btnStartListener);
          System.out.println("on create after main");
          System.out.println("on create before load animals");
  
      }   
  
      private void loadAnimals() {
           System.out.println("########## 1  ");
          _animals = new Animal[3];
  
          // define the display string, the image, and the value to use
          // when the choice is selected
          _animals[0]  = new Animal( "dog", getImg( R.drawable.dog),"dog_sitting" );
          _animals[1]  = new Animal( "cat", getImg( R.drawable.cat),"cat_sitting" );
          _animals[1]  = new Animal( "bird", getImg( R.drawable.bird),"bird_sitting" );
  
      }
  
      private Drawable getImg( int res )
      {
           System.out.println("########## 3  ");
          Drawable img = getResources().getDrawable( res );
          img.setBounds( 0, 0, 48, 48 );
          return img;
      }
  
      public void handlePush( View target ) {
       System.out.println("##########  button clicked handle push");
      // define the list adapter with the choices
          ListAdapter adapter = new AnimalAdapter( this, _animals );
  
          final AlertDialog.Builder ad = new AlertDialog.Builder(this);
          // define the alert dialog with the choices and the action to take
          // when one of the choices is selected
          ad.setSingleChoiceItems( adapter, -1, new OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  // a choice has been made!
                  String selectedVal = _animals[which].getVal();
                  Log.d(TAG, "chosen " + selectedVal );
                  dialog.dismiss();
              }
          });
          ad.show();
     }
  
      static class AnimalAdapter extends ArrayAdapter&lt;Animal&gt; {
  
          private static final int RESOURCE = R.layout.animal_text;
          private LayoutInflater inflater;
  
          static class ViewHolder {
              CheckedTextView nameTxVw;
          }
  
          //@SuppressWarnings("unchecked")
          public AnimalAdapter(Context context, Animal[] objects)
          {
              super(context, RESOURCE, objects);
              inflater = LayoutInflater.from(context);
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent)
          {
              count=count+1;
               System.out.println("################ "+count+" position  "+position+" parent "+parent);
              ViewHolder holder;
              System.out.println("##########  getview outside if 1 "+count);
              if ( convertView == null ) {
                   System.out.println("##########  getview if 1 "+count);
                  // inflate a new view and setup the view holder for future use
                  convertView = inflater.inflate( RESOURCE, null );
  
                  holder = new ViewHolder();
                  holder.nameTxVw =
                      (CheckedTextView) convertView.findViewById(R.id.miastorow_text);
                  convertView.setTag( holder );
              }  else {
                  System.out.println("##########  getview else 1 "+count+"  convert "+convertView.toString());
                  // view already defined, retrieve view holder
                  holder = (ViewHolder) convertView.getTag();
              }
  
              Animal cat = (Animal) getItem( position );
              if ( cat == null ) {
                  System.out.println("##########  getview if 2");
                  //Log.e( TAG, “Invalid category for position: ”+position);
              }
              holder.nameTxVw.setText( cat.getName() );
              holder.nameTxVw.setCompoundDrawables( cat.getImg(), null, null, null );
  
              return convertView;
          }
      }
  
  
  
      class Animal {
          private String   _name;
          private Drawable _img;
          private String   _val;
  
          public Animal( String name, Drawable img, String val ) {
  
               System.out.println("##########  2");
              _name = name;
              _img = img;
              _val = val;
          }
  
          public String getName() {
              return _name;
          }
  
          public Drawable getImg() {
              return _img;
          }
  
          public String getVal() {
              return _val;
          }
      }    
  
  }
  </code></pre>
  
  <p>I am getting a <strong>Null Pointer Exception</strong> .. I dont know how to resolve it, as iam new to Android.. PLZ Help me as soon as possible.</p>
  
  <p>And My logcat details are as follows</p>
  
  <pre><code>07-26 12:14:12.436: DEBUG/AndroidRuntime(13275): Shutting down VM
  07-26 12:14:12.436: WARN/dalvikvm(13275): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275): FATAL EXCEPTION: main
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275): java.lang.NullPointerException
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at com.imagetest.ImageActivity$AnimalAdapter.getView(ImageActivity.java:130)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.AbsListView.obtainView(AbsListView.java:1409)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.ListView.onMeasure(ListView.java:1127)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.View.measure(View.java:8526)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3228)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.View.measure(View.java:8526)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3228)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.View.measure(View.java:8526)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3228)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.View.measure(View.java:8526)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3228)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.View.measure(View.java:8526)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.ViewRoot.performTraversals(ViewRoot.java:902)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.os.Handler.dispatchMessage(Handler.java:99)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.os.Looper.loop(Looper.java:150)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at android.app.ActivityThread.main(ActivityThread.java:4277)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at java.lang.reflect.Method.invokeNative(Native Method)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at java.lang.reflect.Method.invoke(Method.java:507)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  07-26 12:14:12.456: ERROR/AndroidRuntime(13275):     at dalvik.system.NativeStart.main(Native Method)
  07-26 12:14:12.476: WARN/ActivityManager(124):   Force finishing activity com.imagetest/.ImageActivity
  07-26 12:14:12.976: WARN/ActivityManager(124): Activity pause timeout for HistoryRecord{4081ff48 com.imagetest/.ImageActivity}
  07-26 12:14:13.056: INFO/CarouselTimeLog(259): onStart() finish: 1343285053068
  </code></pre>