开发者社区> 问答> 正文

关于android读取联系人时报错

跟着郭神的《第一行代码》写的,学到7.2.2节的读取联系人时出了点问题。

代码如下:

public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    ListView contactsView;
    ArrayAdapter <String> adapter;
    List<String> contactsList = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contactsView = (ListView) findViewById(R.id.contacts_view);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
        contactsView.setAdapter(adapter);
        readContacts();
    }

    private void readContacts()
    {
        Cursor cursor = null;
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
            while (cursor.moveToNext())
            {
// 获取联系人姓名
                String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// 获取联系人手机号
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactsList.add(displayName + "\n" + number);
                if (cursor != null)
                {
                    cursor.close();
                }
            }

    }
}



 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contacts_view">

    </ListView>


</LinearLayout>


 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chalice.sqlite">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>


错误信息:
01-29 05:01:26.758 10406-10406/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.chalice.sqlite, PID: 10406
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chalice.sqlite/com.example.chalice.sqlite.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=10406, uid=10058 requires android.permission.READ_CONTACTS, or grantUriPermission()
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:136)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=10406, uid=10058 requires android.permission.READ_CONTACTS, or grantUriPermission()
                                                       at android.os.Parcel.readException(Parcel.java:1465)
                                                       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
                                                       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
                                                       at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
                                                       at android.content.ContentResolver.query(ContentResolver.java:461)
                                                       at android.content.ContentResolver.query(ContentResolver.java:404)
                                                       at com.example.chalice.sqlite.MainActivity.readContacts(MainActivity.java:49)
                                                       at com.example.chalice.sqlite.MainActivity.onCreate(MainActivity.java:27)
                                                       at android.app.Activity.performCreate(Activity.java:5231)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:136) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                       at dalvik.system.NativeStart.main(Native Method)

展开
收起
爵霸 2016-03-16 11:18:32 3114 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载