面对 java.lang.RuntimeException:在 getActivity().getContentResolver().query() 上传递结果 ResultInfo 失败

Facing java.lang.RuntimeException: Failure delivering result ResultInfo on getActivity().getContentResolver().query()

我正在对 select 联系人使用隐式意图并调用它,但我遇到以下异常,它在我调用 getActivity().getContactResolver().query() 时发生。我分别单独检查了这个调用并确保其中 none 为空。

这是从 selected 联系人恢复信息的代码:

    public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode != Activity.RESULT_OK)
        return;
    else if (requestCode == REQUEST_DATE_CODE)
    {
        GregorianCalendar date = (GregorianCalendar) data.getSerializableExtra(DatePickerFragment.EXTERA_DATE);
        mCrime.setDate(date);
        updateDate();
    }
    else if (requestCode == REQUEST_CONTACT && data != null)
    {
        Uri contactUri = data.getData();
        String[] queryFields = new String[]{ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID};
        Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null);
        try
        {
            if (c.getCount() == 0)
            {
                return;
            }

            c.moveToFirst();
            String suspect = c.getString(0);
            id = c.getInt(1);
            mCrime.setSuspect(suspect);
            mSuspectButton.setText(suspect);
            //mCallSuspectButton.setEnabled(true);
        }
        finally
        {
            c.close();
        }

    }


    /*
    Retriving the phone number
     */
    super.onActivityResult(requestCode, resultCode, data);

}

从 selected 联系人检索 phone 号码的代码:

        mCallSuspectButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] fields = {ContactsContract.CommonDataKinds.Phone.NUMBER};
            //String whereClause = ContactsContract.CommonDataKinds.Phone._ID + " = ?";
            String whereClause = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =  ?";
            String[] args = {Integer.toString(id)};
            Cursor cursor = getActivity().getContentResolver().query(contentUri, fields, whereClause, args, null);
            try
            {
                if (cursor.getCount() == 0)
                {
                    return;
                }
                cursor.moveToFirst();
                String number = cursor.getString(0);
                Uri phoneNumber = Uri.parse("tel:" + number);
                Intent intent = new Intent(Intent.ACTION_DIAL, phoneNumber);
                startActivity(intent);
            }
            finally
            {
                cursor.close();
            }

        }
    });

以及由getActivity().getContactResolver().query()

引起的异常
12 07:04:40.104 22246-22246/com.example.yahya.criminalintent E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.yahya.criminalintent, PID: 22246
                                                                              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/1394i351ae9650f3502e4.1394i6e48faf708f8d3e8.3789r2291-4D2943295B3B2943/2331 flg=0x1 }} to activity {com.example.yahya.criminalintent/com.example.yahya.criminalintent.CrimePagerActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.SemcContactsProvider2 uri content://com.android.contacts/data/phones from pid=22246, uid=10358 requires android.permission.READ_CONTACTS, or grantUriPermission()
                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4185)
                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4228)
                                                                                  at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1564)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:241)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6274)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                               Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.SemcContactsProvider2 uri content://com.android.contacts/data/phones from pid=22246, uid=10358 requires android.permission.READ_CONTACTS, or grantUriPermission()
                                                                                  at android.os.Parcel.readException(Parcel.java:1689)
                                                                                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                                  at android.content.ContentProviderProxy.query(ContentProviderNative.java:427)
                                                                                  at android.content.ContentResolver.query(ContentResolver.java:534)
                                                                                  at android.content.ContentResolver.query(ContentResolver.java:475)
                                                                                  at com.example.yahya.criminalintent.CrimeFragment.onActivityResult(CrimeFragment.java:355)
                                                                                  at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
                                                                                  at android.app.Activity.dispatchActivityResult(Activity.java:6973)
                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4181)
                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4228) 
                                                                                  at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1564) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:241) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6274) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

您需要在清单文件中添加以下权限

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