简体   繁体   中英

android get contact's name and phonenumber for android 1.5

So far I have this code from examples I have seen on here:

public class testLayout extends Activity {
final int PICK_CONTACT = 0;
ImageView image = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    image=(ImageView)findViewById(R.id.icon);
    image.setOnClickListener(onChangePerson);
}

private View.OnClickListener onChangePerson=new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

}
};

@Override
 public void onActivityResult(int reqCode, int resultCode, Intent data) {
 super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
 case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
     Uri contactData = data.getData();
     Cursor c =  managedQuery(contactData, null, null, null, null);
     if (c.moveToFirst()) 
     {
       String name = c.getString(c.getColumnIndexOrThrow(People.NAME));

     }
   }
   break;
   }
   }
   }

This allows me to open up an activity that correctly shows the contacts on the phone, and let's me choose a contact. However, everytime I click on a contact, the program crashes. Any ideas why this is happening? Thanks

Ok so, thank you Hamy for pointing out to me logcat, which I previously had no knowledge of.

It turns out that I was seeing over and over "requires permission READ_CONTACTS" which I had in my android manifest file. Unfortunately, it seems I had it inside of the application tag, which was causing the program to crash without telling me why. Thank you for helping me solve this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM