android读取联系人时报错

问题描述

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)

解决方案

位置放错了

解决方案二:

http://bbs.csdn.net/topics/391898688

解决方案三:

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

解决方案四:

requires android.permission.READ_CONTACTS, or grantUriPermission()

解决方案五:

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

时间: 2024-11-08 18:56:43

android读取联系人时报错的相关文章

android 读取联系人的问题

问题描述 android 读取联系人的问题 android 读取联系人时,我想通过关键字.拼音和电话号码搜索出联系人,应该什么实现,求大神指点.谢谢! 解决方案 在selection加控制就可以了啊

安卓-android代码混淆时报错

问题描述 android代码混淆时报错 android代码混淆时报错,本地库文件在 android_41945.jar 中检测到,可是我根本都不知道 android_41945.jar 是个什么文件,我项目中都找不到,不知怎么解决这种问题. 出错信息如下: [2014-12-18 14:23:52 - EParkV3.0] Native libraries detected in 'android_41945.jar'. See console for more information. [20

eclipse在安装android的ADT时报错 ?

问题描述 eclipse在安装android的ADT时报错 ? 一开始下载了一个ADT 22的显示版本太旧.然后下载23的总报错,报错信息如下:Cannot complete the install because of a conflicting dependency. Software being installed: Tracer for OpenGL ES 23.0.4.1468518 (com.android.ide.eclipse.gldebugger.feature.feature

【我的Android进阶之旅】解决Android Studio启动时报错:Java 1.8 or later is required.

错误描述 在公司电脑上运行Android Studio 2.2已经有一段时间了,但是自己的笔记本上还是用的Android Studio 1.5,今天晚上下了一个Android Studio 2.2压缩包,解压后启动Android Studio 2.2就报错了,错误如下所示: Unsupported Java Version : Cannot start under Java 1.7.0_04-b22 : Java 1.8 or later is required. 分析错误 查看当前电脑的Jav

Android studio运行时报错,方法,类找不到,或者JVM内存溢出解决方案

  Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished wit

Android Contacts(一) 读取联系人

Introduction To Android Contacts Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the Android SQLite and Cursor Article for more information. Google changed the

浅析Android手机卫士读取联系人_Android

推荐阅读: 浅析Android手机卫士sim卡绑定 深入浅析Android手机卫士保存密码时进行md5加密 详解Android 手机卫士设置向导页面 浅析Android手机卫士关闭自动更新 浅析Android手机卫士自定义控件的属性 获取ContentResolver内容解析器对象,通过getContentResolver()方法 调用ContentResolver对象的query()方法,得到raw_contacts表里面的数据,得到Cursor对象 参数:Uri对象,字段String数组 获

android studio 混淆打包时报错,找不到解决方案求帮助啊

问题描述 android studio 混淆打包时报错,找不到解决方案求帮助啊 混淆打包,报错,百度也查不到,困扰1天了... Error:ProGuard: [JinFu1.1] java.io.IOException: Can't read C:Usersadmin.AndroidStudio1.2systemcompile-server_temp_proguard_input0.jar)) Error:ProGuard: [JinFu1.1] at proguard.InputReader

Android控件ListView用法(读取联系人示例代码)_Android

示例代码: 这是一个读取联系人的代码: 复制代码 代码如下: package com.ui.domain; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.database.Cursor; import android.database.DataSetObserver; import android.graphics.Color; import andro