RROR/AndroidRuntime(4522): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(4522): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
这表示,要在activity里面直接调用
如果还不行,那就查看权限,我测试代码如下,android2.3.3
ContentValues values = new ContentValues();
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.GIVEN_NAME, "Sullivan");
values.put(StructuredName.FAMILY_NAME, "Mike");
getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "13989297343");
values.put(Phone.TYPE, Phone.TYPE_HOME);
values.put(Email.DATA, "ligang.02@163.com");
values.put(Email.TYPE, Email.TYPE_WORK);
getContentResolver().insert(Data.CONTENT_URI, values);