Android Message Counter
<p>Hi guys I'm trying to implement an application which will get the contact name and will search for number of messages received from that particular contact name and will display the count. But I'm facing unexpected problem occurred during run of my pgm. here is my code</p>

<pre><code> package com.mycompany.myapp;

import android.app.*;
import android.net.Uri;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.media.*;
import android.telephony.*;
import java.util.*;

import android.content.*;
import android.graphics.*;
import android.database.*;
import android.provider.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    DatePicker datepicker;
    private static Context context;
    Map contactMap;
    String selection;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setTitle("Tuxian");
        setContentView(R.layout.main);
        MainActivity.context = getApplicationContext();

        GetPeopleList people = null;
        final AutoCompleteTextView txt = (AutoCompleteTextView) findViewById(R.id.autocompletetxtview);

        txt.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long rowId) {
                selection = (String)parent.getItemAtPosition(position);
                //TODO Do something with the selected text
            }
        });

        List list = displayContacts(context);
        ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.list_item, list);
        txt.setAdapter(adapter);
        txt.setTextColor(Color.GREEN);
        datepicker = (DatePicker) findViewById(R.id.datePicker);
        Button button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {

                @Override public void onClick(View v)
                {
                    Toast.makeText(getBaseContext(), "Date : " + (datepicker.getMonth() + 1) + "/" + datepicker.getDayOfMonth() + "/" + datepicker.getYear(), Toast.LENGTH_LONG).show();
                    Toast.makeText(getBaseContext(), "Total : " + msgCnt(selection), Toast.LENGTH_SHORT).show();
                }
            });
    }
    private List displayContacts(Context c) {
        ContentResolver cr = c.getContentResolver();
        Cursor pCur = null;
        contactMap = new HashMap&lt;String, String&gt;();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        List l = new ArrayList();
        if (cur.getCount() &gt; 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString( cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) &gt; 0) {
                    pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        //Toast.makeText(getBaseContext(), "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                        l.add(name);                    
                        contactMap.put(name, phoneNo);
                    }
                }
            }
        }
        return l;
    }
    private int msgCnt (String phNum) {

         Uri uri = Uri.parse("content://sms/inbox");

         ContentResolver contentResolver = context.getContentResolver();

         String where = "address="+phNum;
         Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
                                               null);
         int count = cursor.getCount();

        return count;
    }
}
</code></pre>

<p>And I'd added the permission in Manifest file as follows:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="1.0" &gt;

    &lt;uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="11" /&gt;
    &lt;uses-permission android:name="android.permission.READ_SMS"/&gt;

    &lt;application

        android:label="@string/app_name" &gt;
        &lt;activity
            android:label="@string/app_name"
            android:name=".MainActivity" &gt;
            &lt;intent-filter &gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;
    &lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt;
&lt;/manifest&gt;
</code></pre>

<p>Please somebody help me to figure it out! Thanks in advance</p>
