- Java code
package com.chk.cp;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.provider.ContactsContract.PhoneLookup;import android.widget.TextView;public class ContentProviderTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { TextView tv = new TextView(this); String string = ""; super.onCreate(savedInstanceState); // 得到ContentResolver对象 ContentResolver cr = getContentResolver(); // 取得电话本中开始一项的光标 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // 向下移动一下光标 while (cursor.moveToNext()) { // 取得联系人名字 int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); String contact = cursor.getString(nameFieldColumnIndex); // 取得电话号码 int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); String number = cursor.getString(numberFieldColumnIndex); string += (contact + ":" + number + "\n"); } cursor.close(); // 设置TextView显示的内容 tv.setText(string); // 显示到屏幕 setContentView(tv); }}
AndroidManifest.xml
- XML code
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chk.cp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <uses-permission android:name="android.permission.READ_CONTACTS"> </uses-permission> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ContentProviderTestActivity" 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>
错误提示:
- XML code
03-03 21:45:47.649: E/AndroidRuntime(883): FATAL EXCEPTION: main03-03 21:45:47.649: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chk.cp/com.chk.cp.ContentProviderTestActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.ActivityThread.access$600(ActivityThread.java:123)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)03-03 21:45:47.649: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)03-03 21:45:47.649: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:137)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4424)03-03 21:45:47.649: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)03-03 21:45:47.649: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:511)03-03 21:45:47.649: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)03-03 21:45:47.649: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)03-03 21:45:47.649: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)03-03 21:45:47.649: E/AndroidRuntime(883): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.03-03 21:45:47.649: E/AndroidRuntime(883): at android.database.CursorWindow.nativeGetString(Native Method)03-03 21:45:47.649: E/AndroidRuntime(883): at android.database.CursorWindow.getString(CursorWindow.java:442)03-03 21:45:47.649: E/AndroidRuntime(883): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)03-03 21:45:47.649: E/AndroidRuntime(883): at android.database.CursorWrapper.getString(CursorWrapper.java:114)03-03 21:45:47.649: E/AndroidRuntime(883): at com.chk.cp.ContentProviderTestActivity.onCreate(ContentProviderTestActivity.java:29)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.Activity.performCreate(Activity.java:4465)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)03-03 21:45:47.649: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)