开发者

Application stopped working unexpectedly. Please try again

I am trying to display the name and number of a contact from android phone book, i have one contact stored in my AVD. can anyone tell me whats wrong with this code please...it is showing application stopped working Please try again...thanks in advance...

public class get extends Activity {
        private void getColumnData(Cursor cur)
    {
        if(cur.moveToFirst())
        {
            int numi=cur.getColumnIndex(People.NUMBER);
            int namei=cur.getColumnIndex(People.NAME);
    开发者_JAVA百科        String name,num;
            do{
                name=cur.getString(namei);
                num=cur.getString(numi);

            }while(cur.moveToNext());
        }
    }
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Uri u=People.CONTENT_URI;
        String[] proj=new String[]{People.NAME,People.NUMBER};
        Cursor cur=managedQuery(u, proj, null, null, null);
        getColumnData(cur);
        TextView tv=new TextView(this);
        tv.setText("First step");
        setContentView(tv);
    }
}


Try using android.provider.ContactsContract.Contacts;

Also put READ_CONTACTS permission in the Manifest file


import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts;

ListView lv=(ListView)findViewById(R.id.list_view);

Uri uri=Contacts.CONTENT_URI;

String [] projection=new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
Cursor c=managedQuery(uri,null, null, null, null);
String[] from=new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
int[] to=new int[]{android.R.id.text1,android.R.id.text2};
SimpleCursorAdapter sAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, c, from, to);
lv.setAdapter(sAdapter);

Also put in the manifest file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜