当我尝试使用android studio从手机访问联系人时程序崩溃

时间:2017-07-26 17:48:15

标签: android

当我使用这行代码时,程序崩溃了:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //TextView textView=(TextView) findViewById(R.id.Tv);
        //textView.setText(Name);
        TextView textView=(TextView) findViewById(R.id.Tv);
        //ContentResolver contentResolver=getContentResolver();
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        String Name;
        if(cursor.getCount() > 0){
            while(cursor.moveToNext()){
                Name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                textView.setText(Name);
            }
        }
        cursor.close();
    }
}

以下是我遇到的错误:

  

---------崩溃开始07-28 15:01:42.622 2527-2527 / com.example.mateusjose.contacts E / AndroidRuntime:FATAL   EXCEPTION:main Process:com.example.mateusjose.contacts,PID:2527   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.mateusjose.contacts / com.example.mateusjose.contacts.MainActivity}:   java.lang.SecurityException:Permission Denial:打开提供者   来自的com.android.providers.contacts.ContactsProvider2   ProcessRecord {5be2630 2527:com.example.mateusjose.contacts / u0a85}   (pid = 2527,uid = 10085)需要android.permission.READ_CONTACTS或   android.permission.WRITE_CONTACTS在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)   在android.app.ActivityThread.-wrap12(ActivityThread.java)处   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)   在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:154)at   android.app.ActivityThread.main(ActivityThread.java:6077)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)引起   by:java.lang.SecurityException:Permission Denial:打开提供者   来自的com.android.providers.contacts.ContactsProvider2   ProcessRecord {5be2630 2527:com.example.mateusjose.contacts / u0a85}   (pid = 2527,uid = 10085)需要android.permission.READ_CONTACTS或   android.permission.WRITE_CONTACTS在   android.os.Parcel.readException(Parcel.java:1683)at   android.os.Parcel.readException(Parcel.java:1636)at   android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4169)   在   android.app.ActivityThread.acquireProvider(ActivityThread.java:5434)   在   android.app.ContextImpl $ ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2267)   在   android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1515)   在android.content.ContentResolver.query(ContentResolver.java:514)at   android.content.ContentResolver.query(ContentResolver.java:472)at   com.example.mateusjose.contacts.MainActivity.onCreate(MainActivity.java:29)   在android.app.Activity.performCreate(Activity.java:6662)at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)   在android.app.ActivityThread.-wrap12(ActivityThread.java)处   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)   在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:154)at   android.app.ActivityThread.main(ActivityThread.java:6077)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:0)

您需要在清单上添加此内容:

<uses-permission android:name="android.permission.READ_CONTACTS" />

对于API 23+,您需要获得用户访问联系人的权限。有关如何提问的详细信息,请参阅this

答案 1 :(得分:0)

使用运行时权限并尝试。要查明问题是否是由运行时权限引起的,只需转到设置中的应用信息并检查权限列表。如果已禁用,请关闭联系人权限。