我希望我的应用在Android中修改用户的个人资料联系信息。但是,它会一直显示运行时错误,即NullPointerException。
private void updateUserProfile(Uri uri, Profile profile) {
String mRawContactId = null;
Cursor mContactCursor = getContentResolver().query(uri, null, null, null, null);
if (mContactCursor.moveToFirst()) {
String mContactId = getCursorString(mContactCursor,
ContactsContract.Contacts._ID);
Cursor mRawContactCursor = getContentResolver().query(
RawContacts.CONTENT_URI,
null,
Data.CONTACT_ID + " = ?",
new String[] {mContactId},
null);
Log.v("RawContact", "Got RawContact Cursor");
try {
ArrayList<String> mRawContactIds = new ArrayList<String>();
while(mRawContactCursor.moveToNext()) {
String rawId = getCursorString(mRawContactCursor, RawContacts._ID);
Log.v("RawContact", "ID: " + rawId);
mRawContactIds.add(rawId);
}
for(String rawId : mRawContactIds) {
// Make sure the "last checked" RawContactId is set locally for use in insert & update.
mRawContactId = rawId;
}
} finally {
mRawContactCursor.close();
}
}
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, mRawContactId)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Data.DATA1, profile.getEmail())
.withValue(Email.TYPE, Email.TYPE_HOME)
.withValue(Email.DISPLAY_NAME, "Email")
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我发现它在getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
行。但getContentResolver()
,ContactsContact.AUTHORITY
&amp; ops
可以为空。
谁能告诉我这是什么问题?感谢