I was trying to get the contacts using a query through the ContactsContract
The following code throws an exception.
java.lang.ClassCastException.
public class ContactManager extends Activity{
ArrayList<String> contactsList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] contacts = getContactNames();
}
public String[] getContactNames(){
try{
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
contactsList.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
}
return (String []) contactsList.toArray();
}
catch(Exception e){
Log.e("WOw", e.toString());
return null;
}
}
}
Any idea?
I did not forget to add the permission
The method
toArray()returns an array ofObjectand it cannot be cast to an array ofString.You have to provide an argument to the method to specify the type :
You will find more details in the documentation.