Tôi có một ứng dụng trong đó một khía cạnh là để người dùng chọn một liên hệ và gửi văn bản đến liên hệ đó thông qua ứng dụng. Ứng dụng này chỉ hoạt động với một số liên hệ và không hoạt động với những người khác. Chính xác hơn:Intent.ACTION_PICK trả về con trỏ trống cho một số liên hệ
cho các liên hệ mà tôi đã nhập vào sổ liên lạc của mình bằng tay, Intent.ACTION_PICK
không gặp khó khăn khi tìm và trả lại ứng dụng, tức là cursor.moveToFirst()
là đúng.
Nhưng đối với địa chỉ liên hệ đã được nhập bởi Facebook (điện thoại của tôi được đặt để đồng bộ với danh bạ trên Facebook), tôi nhận được android.database.CursorIndexOutOfBoundsException
sau khi tôi nhấp vào liên hệ. Một câu hỏi rõ ràng tôi có là: tại sao kích thước kết quả là 0 sau khi tôi đã chọn một địa chỉ liên hệ? Tại sao là cursor.moveToFirst()
sai?
...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
Đây là mã của tôi:
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 kết quả:
(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 ...
}
Lưu ý: Tôi thấy các điểm tiếp xúc. Nhưng sau khi tôi chọn địa chỉ liên hệ đã nhập trên Facebook, tôi sẽ gặp sự cố.
BTW: đoạn mã của tôi là sao chép từ các hướng dẫn android chính xác: http://developer.android.com/training/basics/intents/result.html
Lưu ý rằng đối với địa chỉ liên hệ trên Facebook, 'cursor.moveToFirst()' trả về false. Tôi không chỉ đơn thuần quan tâm đến một bandaid như bắt ngoại lệ. Tôi biết cách kiểm tra 'cursor.moveToFirst()'. Những gì tôi cần biết là làm thế nào để có được những con số cho những địa chỉ liên lạc. –
Nhận xét này không liên quan đến câu hỏi của bạn ... Vì bạn đang sử dụng phép chiếu, bạn không cần gọi .getColumnIndex (...). Chỉ mục cột sẽ khớp với chỉ mục của cột đó trong mảng chuỗi chiếu. – abh