Quiero mostrar los contactos de la agenda pero se me cierra la aplicación al instante. No se donde esta el error:
public class Main extends ListActivity {
TextView txt;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (TextView)findViewById(R.id.contactName);
list = (ListView) findViewById(android.R.id.list);
showContacts();
}
protected void showContacts(){
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID;}
int[] to = new int[]{R.id.contactID,R.id.contactName};
txt.setText(ContactsContract.PhoneLookup.CONTENT_FILTER_URI.toString());
//Uri contacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Uri contacts = ContactsContract.Contacts.CONTENT_URI;
//Uri contacts = Uri.parse("content://contacts/people");
Cursor c = getContentResolver().query(contacts,projection,null,null,null);
ListAdapter sca = new SimpleCursorAdapter(Main.this,android.R.layout.activity_list_item,c,projection,to);
list.setAdapter(sca);
Error:
FATAL EXCEPTION: main Process: es.exitae.listadocontactos7, PID: 4184 java.lang.RuntimeException: Unable to start activity ComponentInfo{es.exitae.listadocontactos7/es.exitae.listadocontactos7.Main}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{98c9152 4184:es.exitae.listadocontactos7/u0a73} (pid=4184, uid=10073) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
El error continua, yo no entiendo mucho pero por lo poco que veo es que me esta diciendo que añada permisos en el android manifest y eso ya lo tengo hecho
Este es mi android manifest:
<uses-permission android:name="android.permission.READ_CONTACTS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS" ></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>