高分 请熟悉getContentResolver的朋友帮我看一段异常代码!!
是关于ContentProvider的,读取和插入手机上的通讯录信息,代码很好读懂的,关键是我不知道缺少啥东西了,跑不起来,代码如下:
先看ContentProviderDemo.java的内容:
package com.MyTest.ContentProviderDemo;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.util.Log;
import android.widget.Toast;
public class ContentProviderDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InsertRecords("wangming","13921546287");
displayRecords();
}
private void InsertRecords(String name,String phoneNo)
{
ContentValues values = new ContentValues();
values.put(People.NAME, name);
Uri uri = getContentResolver().insert(People.CONTENT_URI, values); //错误1
Log.d("ANDROID", uri.toString());
Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.NUMBER, phoneNo);
getContentResolver().insert(numberUri, values);
}
private void displayRecords()
{
String columns[] = new String[] {People.NAME,People.NUMBER};
Uri mContacts = People.CONTENT_URI;
Cursor cur = managedQuery(mContacts,
columns, //要返回的数据字段
null, //WHERE子句
null, //WHERE子句的参数
null); //ORDER子句
if (cur.moveToFirst())
{
String name = null;
String phoneNo = null;
do
{
name = cur.getString(cur.getColumnIndex(People.NAME));
phoneNo = cur.getString(cur.getColumnIndex(People.NUMBER));
Toast.makeText(this, name + " " + phoneNo, Toast.LENGTH_LONG).show();
}while(cur.moveToNext());
}
}
}
再贴Androidmanifest.xml的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MyTest.ContentProviderDemo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.READ_CONTACT"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContentProviderDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我单步跟踪调试了下,发现"错误1"(上面代码标注处)处提示 "source not found",这段代码看似简单,但是问题到底出在哪里呢,请指点下 :)