Android managedQuery() IllegalArgumentException on opening returned URI path to a single contact
-
29-10-2019 - |
题
I'm having trouble with a tutorial application from Professional Android 2 Application Development. I'm developing for android 2.3, and it seems like the code has changed a good bit in revisions.
Basically I have two activities, one that launches the other, to get a list of contacts and then to return the contact chosen back to the first activity which will then display it on screen, I know android has something that does this already, but I'm trying to learn here :p
All communication between the activities is handled using intents. The first activity starts up the other activity fine, I can pick a contact and it returns it to the first activity. It runs onReturnActivity()
all fine. But when I try to create a cursor(Using managedQuery()
) in onReturnActivity()
to open the single returned contacts database entry, everything goes pearshaped and I get an IllegalArgumentException.
That returned URI path is: content://com.android.contacts/contacts15 (Or another number).
The Error is:
java.lang.RuntimeException: Failure delivering result ResultInfo {
who=null, request=1, result=-1, data=Intent {
dat=content://com.android.contacts/contacts15 }}
to activity {com.paad.contactPicker/com.paad.contactPicker.ContactPickerTester}:
java.lang.IllegalArgumentException:
URI: content://com.android.contacts/contacts15,
calling user: com.paad.contactPicker,
calling package:com.paad.contactPicker
Creating the cursor in the other activity works okay though, so the only things I can think (With my very limited knowledge of Android) is that it's a problem with the uri path or with the entry returned from the other activity not containing anything. But I'm more inclined to believe it's the first.
All permissions set and I can access the contacts database in the other activity fine.
Would really appreciate some help on this, I've been stuck for the past 2 days :/
EDIT
Here's calling the second activity:
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://android.provider.ContactsContract")) ;
startActivityForResult(intent, PICK_CONTACT) ;
}
I don't actually use the URI I pass in to this Intent though, I have it hardcoded into the second activity as this:
final Uri data = ContactsContract.Contacts.CONTENT_URI ;
And here's where I call managedQuery() the first time (where it works):
final Cursor c = managedQuery(data, null, null, null, null) ;
Here's the onReturn Activity: public void onActivityResult(int reqCode, int resCode, Intent data) { super.onActivityResult(reqCode, resCode, data) ;
switch(reqCode) {
case(PICK_CONTACT) : {
if(resCode == Activity.RESULT_OK) {
Uri contactData = data.getData() ;
Cursor c = managedQuery(contactData, null, null, null, null) ;
c.moveToFirst() ;
String name = c.getString(c.getColumnIndexOrThrow("DISPLAY_NAME")) ;
TextView tv = (TextView)findViewById(R.id.textView2) ;
tv.setText(name) ;
}
}
break ;
}
}
没有正确的解决方案