2013-05-16 4 views
13

Ich habe eine App, in der ein Benutzer einen Kontakt auswählen und einen Text an diesen Kontakt über die App senden kann. Die App funktioniert nur mit einigen Kontakten und schlägt bei anderen fehl. Genauer gesagt:Intent.ACTION_PICK gibt einen leeren Cursor für einige Kontakte zurück

für die Kontakte, die ich in mein Kontaktbuch von Hand eingegeben, hat die Intent.ACTION_PICK keine Probleme zu finden und zurück zur App, d. H. cursor.moveToFirst() ist wahr.

Aber für den Kontakt, die von Facebook importiert wurden (mein Telefon ist so eingestellt, mit Facebook-Kontakten zu synchronisieren), bekomme ich folgende android.database.CursorIndexOutOfBoundsException, nachdem ich auf den Kontakt geklickt habe. Eine krasse Frage, die ich habe, ist: Warum ist die Ergebnisgröße 0, nachdem ich buchstäblich einen Kontakt ausgewählt habe? Warum ist cursor.moveToFirst() falsch?

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

Hier ist mein Code:

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 Ergebnis:

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

Hinweis: Ich sehe die Kontakte. Aber nachdem ich den Facebook importierten Kontakt ausgewählt habe, bekomme ich den Absturz.

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

+0

Beachten Sie, dass 'cursor.moveToFirst()' für die Facebook-Kontakte 'false zurückgibt. Ich bin nicht nur an einem Bandaid interessiert, wie zum Beispiel an der Ausnahme. Ich weiß, wie man 'cursor.moveToFirst()' überprüft. Was ich wissen muss, ist, wie man die Nummern für diese Kontakte bekommt. –

+0

Dieser Kommentar hat nichts mit Ihrer Frage zu tun ... Da Sie eine Projektion verwenden, müssen Sie .getColumnIndex (...) nicht aufrufen. Der Spaltenindex stimmt mit dem Index dieser Spalte im Projektionszeichenfolgenfeld überein. – abh

Antwort

2

Sie können nicht darauf zugreifen FB Kontakte über die API Kontakte: ist mein Code-Schnipsel aus dem Android-Tutorial genau kopiert.

2

den Absturz zu beheben, sollten Sie das Ergebnis moveToFirst überprüfen() wie folgt aus:

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

Um die Art der Daten zu untersuchen, die Ihnen zur Verfügung ist, würde ich so in „Null“ für die Projektion übergeben dass alle Felder zurückkommen und die Feldnamen und -werte ausgeben. Sie finden die Daten, die Sie suchen, nur nicht im Feld NUMMER.

Verwandte Themen