本帖最后由 mengxiang1993 于 2015-3-29 22:20 编辑
在张泽华老师的安卓基础视频第47讲,保存联系人到系统通讯录
我的代码如下:
- package com.example.writeContect;
- import android.app.Activity;
- import android.app.ActionBar;
- import android.app.Fragment;
- import android.content.ContentResolver;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Toast;
- import android.os.Build;
- /**
- * 需求:向系统通讯录添加一个联系人
- * 步骤:
- * 1.向raw_contacts表添加一个id
- * 2.通过查询raw_contacts表获取_id的值,然后_id+1插入到raw_contacts表
- * 3.向data表添加data1的内容和mimetype类型
- *
- */
- public class MainActivity extends Activity {
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_main);
- }
-
- public void click(View view){
- ContentResolver resolver=getContentResolver();
- Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
- Uri dataUri=Uri.parse("content://com.android.contacts/data");
-
- Cursor cursor=resolver.query(uri, new String[]{"_id"}, null, null, null);
-
- cursor.moveToLast();
-
-
- int _id=cursor.getInt(0);
- ContentValues values=new ContentValues();
- values.put("contact_id", _id+1);
- resolver.insert(uri, values);
-
- Toast.makeText(this, "保存成功", 0).show();
-
-
- Cursor cursor2=resolver.query(dataUri,null, null, null, null);
- ContentValues telValues=new ContentValues();
- telValues.put("data1", "110");
- telValues.put("mimetype","vnd.android.cursor.item/phone_v2");
- resolver.insert(dataUri, telValues);
-
-
- ContentValues telValues2=new ContentValues();
-
- telValues2.put("data1", "119");
- telValues2.put("mimetype","vnd.android.cursor.item/phone_v2");
- resolver.insert(dataUri, telValues2);
-
- ContentValues nameValues=new ContentValues();
- nameValues.put("data1", "wangwu");
- nameValues.put("mimetype","vnd.android.cursor.item/name");
- resolver.insert(dataUri, nameValues);
-
- }
- }
复制代码
报这行有错: telValues.put("mimetype","vnd.android.cursor.item/phone_v2");
求大神指点:哪里来的空指针,。怎么解决?
|
|