qq_27914019 2016-01-29 10:20 采纳率: 100%
浏览 1908
已采纳

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条回答 默认 最新

  • 小菜陈 2016-01-30 06:19
    关注

    位置放错了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?