我有一个应用程序,其中一个方面是一种用于用户选择一个联系人,并发送文本到该联系人通的应用程序。 该程序只适用于一些接触和失败别人。 更确切地说:
对于我的手进入了我的通讯录联系人时, Intent.ACTION_PICK
有任何麻烦找到并返回给应用程序,即cursor.moveToFirst()
是正确的。
但对于被Facebook导入的联系人(我的手机设置与Facebook联系人同步),我得到以下android.database.CursorIndexOutOfBoundsException
后,我点击联系人。 一个明显的问题,我有是:为什么是结果大小为0后,我从字面上选择的联系? 为什么cursor.moveToFirst()
假的?
...Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
05-15 17:57:04.741: E/AndroidRuntime(21301): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
05-15 17:57:04.741: E/AndroidRuntime(21301): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-15 17:57:04.741: E/AndroidRuntime(21301): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-15 17:57:04.741: E/AndroidRuntime(21301): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-15 17:57:04.741: E/AndroidRuntime(21301): at com.company.Game.SendTextActivity.onActivityResult(SendTextActivity.java:118)
05-15 17:57:04.741: E/AndroidRuntime(21301): at android.app.Activity.dispatchActivityResult(Activity.java:5436)
05-15 17:57:04.741: E/AndroidRuntime(21301): at android.app.ActivityThread.deliverResults(ActivityThread.java:3188)
05-15 17:57:04.741: E/AndroidRuntime(21301): ... 11 more
这里是我的代码:
dispatchIntent:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
onActivity结果:
(requestCode == PICK_CONTACT_REQUEST) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = { Phone.NUMBER };
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
//do work with number here ...
}
注:我看到的联系人。 但之后,我选择了Facebook导入的联系人,我得到的崩溃。
BTW:我的代码片段被从Android教程复制正是: http://developer.android.com/training/basics/intents/result.html