2013-05-16 3 views
13

사용자가 연락처를 선택하고 해당 연락처로 텍스트를 보낼 수있는 앱이 있습니다. 앱은 일부 연락처에서만 작동하며 다른 연락처에서는 실패합니다. 더 정확하게 :Intent.ACTION_PICK는 일부 연락처에 대해 빈 커서를 반환합니다.

나는 손으로 내 연락처 책에 입력 된 연락처를 들어, Intent.ACTION_PICK는 아무런 문제 cursor.moveToFirst() 사실 즉, 발견하고 응용 프로그램에 반환이 없습니다.

페이스 북에서 가져온 연락처 (내 핸드폰이 Facebook 연락처와 동기화되도록 설정 됨)를 클릭하면 연락처를 클릭 한 후 다음과 같이 나타납니다. android.database.CursorIndexOutOfBoundsException 내가 가지고있는 눈부신 질문 중 하나는 문자 그대로 연락처를 선택한 후 왜 결과 크기가 0입니까? cursor.moveToFirst()은 왜 거짓입니까?

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 ... 
     } 

참고 : 나는 연락처를 볼 수

...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 

여기 내 코드입니다. 하지만 Facebook에서 가져온 연락처를 선택하면 충돌이 발생합니다.

은 BTW : http://developer.android.com/training/basics/intents/result.html

+0

Facebook 연락처의 경우 'cursor.moveToFirst()'는 false를 반환합니다. 나는 예외를 잡는 것과 같은 무표정에 관심이있다. 나는'cursor.moveToFirst()'를 체크하는 방법을 안다. 내가 알아야 할 것은 그 연락처에 대한 번호를 얻는 방법입니다. –

+0

이 주석은 귀하의 질문과 관련이 없습니다 ... 투영법을 사용하고 있으므로 .getColumnIndex (...)를 호출 할 필요가 없습니다. 열 인덱스는 투영 문자열 배열의 해당 열의 인덱스와 일치합니다. – abh

답변

2

당신은 연락처 API를 통해 FB 연락처에 액세스 할 수 없습니다 : 내 코드는 정확히 안드로이드 튜토리얼에서 복사됩니다.

String number = null; 
if (cursor.moveToFirst()) { 
    number = cursor.getString(0); // 0 matches the index of NUMBER in your projection. 
} 

내가 그렇게 투사은 "널 (null)"에 전달할 것이다 당신에게 사용할 수있는 데이터의 특성을 탐색하려면 :

2

이 같은) (moveToFirst의 결과를 확인해야 충돌을 해결하려면 모든 필드가 다시 돌아오고 필드 이름과 값을 덤프합니다. 찾고있는 데이터를 찾을 수 있습니다. NUMBER 필드가 아닙니다.

관련 문제