Ich schreibe eine Anwendung, die Kontakte in die SIM-Karte eines Android-Telefons schreibt. Ich bin an dem Punkt fest, wo die Telefonnummer hinzugefügt wird: eine Ausnahme tritt ohne ersichtlichen Grund auf.Android: Wie fügt man einen Kontakt zur SIM mit dem SDK hinzu?
Hier ist ein Code-Schnipsel.
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContactsEntity;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.RawContacts.Entity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
[...]
try{
// add a row to the RawContacts table
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim");
values.put(RawContacts.ACCOUNT_NAME, "SIM");
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
// get the ID of the newly-added line
long rawContactId = ContentUris.parseId(rawContactUri);
// add a "name" line to the Data table, linking it to the new RawContact
// with the CONTACT_ID column
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Name");
cr.insert(Data.CONTENT_URI, values);
// this insert succeeds
// add a "phone" line to the Data table, linking it to the new RawContact
// with the CONTACT_ID column
values.clear();
values.put(Data.CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "+12345678901");
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
cr.insert(Data.CONTENT_URI, values);
// this insert fails with a NullPointerException
}
catch(Exception e){
String xx=e.toString();
System.out.println(xx);
}
Die Anwendung verfügt über Berechtigungen android.permission.READ_CONTACTS und android.permission.WRITE_CONTACTS.
Das Telefon zeigt einen Kontakt mit dem Namen, aber kein Telefon (das Hinzufügen des Telefons zu diesem Kontakt mit der normalen Benutzeroberfläche führt dazu, dass ein neuer Kontakt mit Name und Telefon hinzugefügt wird und der alte Kontakt mit dem Namen nur bleibt) .
Irgendeine Idee warum die dritte Einfügung (die zweite in der Datentabelle) fehlschlägt, während die 2 vorherigen (1 in RawContacts und 1 in Data) erfolgreich sind?
Es wird wirklich hilfreich sein, wenn Sie den Exception-Stack-Trace zu – ognian
schreiben Sie meinen, einen Haltepunkt in die Fanganweisung setzen und den Stapel einfügen Trace, wenn der Haltepunkt erreicht wird (dh die Ausnahme wurde abgefangen)? Können Sie sehen, welche Funktion die Ausnahme ausgelöst hat? jedoch ersetzt Data.CONTACT_ID mit Data.RAW_CONTACT_ID gelöst. – user377486