title: how to show the contacts using AutoCompleteTextView android?
body: <p>i want to show contacts using AutoCompleteTextView  in my application. I googled and using some code from stack overflow but some times it is not working well and some times it is crashing too. Please any one help me in this.</p>
  
  <p>custcontview.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card_view="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
  
      android:layout_margin="10dp"
      android:padding="10dp"
      card_view:cardBackgroundColor="#ffffff"
      card_view:cardCornerRadius="1dp"
      android:layout_marginLeft="15dp"
  
      card_view:cardElevation="5dp"
  
      card_view:contentPadding="2dp"&gt;
  
  &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
      android:layout_marginTop="10dp"
      android:layout_marginLeft="10dp"
      android:layout_marginBottom="10dp"
              android:weightSum="2"&gt;
      &lt;TextView
          android:id="@+id/ccontName"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Large Text"
          android:layout_weight="1"
          android:textSize="15sp"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
      &lt;TextView
          android:id="@+id/ccontNo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@id/ccontName"
          android:text="Medium Text"
          android:layout_weight="1"
         android:textSize="15sp"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
          &lt;/LinearLayout&gt;
  
      &lt;TextView
          android:id="@+id/ccontType"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_above="@id/ccontNo"
          android:layout_alignParentRight="true"
          android:layout_marginRight="14dp"
          android:text="Small Text"
          android:textSize="15sp"
          android:visibility="gone"
          android:textColor="#A5000000"
          android:textAppearance="?android:attr/textAppearanceSmall" /&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>mianfragment.java:</p>
  
  <pre><code> AutoCompleteTextView mobile_prepaid_mobilenum_edttxt;
     private ArrayList&lt;Map&lt;String, String&gt;&gt; mPeopleList;
  </code></pre>
  
  <p>oncreate:   </p>
  
  <pre><code>mPeopleList = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
  mobile_prepaid_mobilenum_edttxt = (AutoCompleteTextView) lay.findViewById(R.id.mobile_prepaid_mobilenum_edttxt);
          new Task1().execute();
          mAdapter = new SimpleAdapter(getActivity(), mPeopleList, R.layout.custcontview ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
  
          mobile_prepaid_mobilenum_edttxt.setAdapter(mAdapter);
          mobile_prepaid_mobilenum_edttxt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; av, View arg1, int index, long arg3) {
                  Map&lt;String, String&gt; map = (Map&lt;String, String&gt;) av.getItemAtPosition(index);
                  String name = map.get("Name");
                  String number = map.get("Phone");
                  mobile_prepaid_mobilenum_edttxt.setText(number);
              }
          });
  }
   public void PopulatePeopleList()
      {
  
          mPeopleList.clear();
  
          Cursor people = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  
          while (people.moveToNext())
          {
              String contactName = people.getString(people.getColumnIndex(
                      ContactsContract.Contacts.DISPLAY_NAME));
  
              String contactId = people.getString(people.getColumnIndex(
                      ContactsContract.Contacts._ID));
              String hasPhone = people.getString(people.getColumnIndex(
                      ContactsContract.Contacts.HAS_PHONE_NUMBER));
  
              if ((Integer.parseInt(hasPhone) &gt; 0))
              {
  
                  // You know have the number so now query it like this
                  Cursor phones = getActivity().getContentResolver().query(
                          ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                          null,
                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
                          null, null);
                  while (phones.moveToNext()) {
  
                      //store numbers and display a dialog letting the user select which.
                      String phoneNumber = phones.getString(
                              phones.getColumnIndex(
                                      ContactsContract.CommonDataKinds.Phone.NUMBER));
  
                      String numberType = phones.getString(phones.getColumnIndex(
                              ContactsContract.CommonDataKinds.Phone.TYPE));
  
                      Map&lt;String, String&gt; NamePhoneType = new HashMap&lt;String, String&gt;();
  
                      NamePhoneType.put("Name", contactName);
                      NamePhoneType.put("Phone", phoneNumber.replace("-",""));
  
                      if(numberType.equals("0"))
                          NamePhoneType.put("Type", "Work");
                      else
                      if(numberType.equals("1"))
                          NamePhoneType.put("Type", "Home");
                      else if(numberType.equals("2"))
                          NamePhoneType.put("Type",  "Mobile");
                      else
                          NamePhoneType.put("Type", "Other");
  
                      //Then add this map to the list.
                      mPeopleList.add(NamePhoneType);
                  }
                  phones.close();
              }
          }
          people.close();
      getActivity(). startManagingCursor(people);
  
  
      }
  
  
      class Task1 extends AsyncTask&lt;Void, Void, String&gt; {
  
          @Override
          protected void onPreExecute()
          {
              super.onPreExecute();
          }
          @Override
          protected String doInBackground(Void... arg0)
          {
              //Record method
              PopulatePeopleList();
  
  
  
              return "name";
          }
  
          @Override
          protected void onPostExecute(String result)
          {
              super.onPostExecute(result);
  
          }
      }
  </code></pre>
  
  <p>ERROR:</p>
  
  <pre><code> AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
          Process: com.reload.reload, PID: 17564
          java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:304)
          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:818)
          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.support.v4.app.FragmentActivity.getContentResolver()' on a null object reference
          at com.reload.reload.activity.MobileFragment.PopulatePeopleList(MobileFragment.java:1034)
          at com.reload.reload.activity.MobileFragment$Task1.doInBackground(MobileFragment.java:1087)
          at com.reloadapp.reload.activity.MobileFragment$Task1.doInBackground(MobileFragment.java:1076)
          at android.os.AsyncTask$2.call(AsyncTask.java:292)
          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:818)
  </code></pre>