0

I'm a complete noob in android app development, so I need some help from any expert out there. I found some code online to display the contact list when clicking on a EditText. I understand most of the code so far but the problem is when I select the contact, it just throws up a force close error.

What I would like it to do is, once clicking on a contact, the contact's name and number is added to the EditText with ";" as a separator.

Here's the code I'm working with:

package com.rmedia.android;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class ContactList extends Activity implements OnClickListener {
    EditText ed;
    int PICK_CONTACT;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed =(EditText)findViewById(R.id.editContact);
        ed.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.editContact:
                 Intent intent = new Intent(Intent.ACTION_PICK);
                 intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                 startActivityForResult(intent, PICK_CONTACT);
            break;
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent){
        if (requestCode == PICK_CONTACT){         
            Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
            cursor.moveToNext();

            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

            Toast toast = Toast.makeText(this, "Contect LIST  =  "+name+" "+contactId, Toast.LENGTH_LONG);
            toast.show();
        }
    }
}

Any help towards the right direction is greatly appreciated! Thanks!

SReca
  • 643
  • 3
  • 13
  • 37
  • 1
    look at/post the logcat error message, sigh.... – james Apr 08 '11 at 19:02
  • I checked out the LogCat, this is what I fount - "Caused by: Java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content" - What does this mean? I have the permissions on the AndroidManifest.xml – SReca Apr 08 '11 at 19:09
  • there was a typo in the manifest file - `` needed to use "name" instead of "id". – SReca Apr 08 '11 at 19:50

1 Answers1

1

I used to get the same problem as you are getting with my SMS app as well. And then it struck to me, that, whenever an app needs to get permissions, and if I don't include what permission should I need to run this app, Android forcibly close the app before even I can blink.

So before launching any app, whether you are copying from someone else's code or from Internet or you are doing it on your own,

  • always think (more than) twice about whether your app needs to declare any permissions in your Manifest file
  • if so, declare it before attempting to run your app in an emulator or on a real device.
olasitha
  • 66
  • 2