I cannot make any solution work... The result is always an app crash. I successfully made a listView, added items and got the selected ones, but I miss making the elements of the list the contacts' names instead of random ones.
Once selected, I will ask the number and PutExtra them in another activity.
EDIT: As suggested, here's the code I'm trying to get working:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
I'll insert this into a for loop for every contact, adding every time the strings name and phoneNumber to an ArrayList. However, when I press the button that should run this code (and the button is working fine), I get a long list of errors from the Android Monitor (here's the first lines):
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
I've added the permission to the manifest:
<uses-permission android:name="android.permission.READ_CONTACTS" />
Could you please help me? Thanks!
P.s. I've tryed multiple solutions from this previus topic, none of them work for me...