2 qq 27914019 qq_27914019 于 2016.01.29 18:20 提问

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)

5个回答

zhanghaoyangchao
zhanghaoyangchao   2016.01.30 14:19
已采纳

位置放错了

zhanghaoyangchao
zhanghaoyangchao 编译能通过?
接近 2 年之前 回复
devmiao
devmiao   Ds   Rxr 2016.01.29 22:34
zrf1335348191
zrf1335348191   2016.01.30 08:34

错误原因是说让你加几个权限,按照他给的错误加权限就行了

crazy1235
crazy1235   Ds   Rxr 2016.01.31 20:37

requires android.permission.READ_CONTACTS, or grantUriPermission()

91program
91program   Ds   Rxr 2016.01.29 22:33

权限问题,需要设置一下minifast文件

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!