2015-11-20 27 views
0

所以Android清单验证错误,我试图使用内置的联系人选择器在我的Android应用程序,但我得到了这条线以下错误:与使用的权限

cursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 

错误:

Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=11135, uid=10057 requires android.permission.READ_CONTACTS, or grantUriPermission() 

所以,我想我必须添加使用许可权在我的AndroidManifest.xml像这样(这是我的清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="balevski.meetme" > 
<uses-permission android:name="android.permission.READ_CONTACTS"/> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
</manifest> 

因此,我在添加READ_CONTACTS权限后重建了我的项目,但仍然遇到同样的问题。

接下来,我验证了我AndroidManifest.xml文件,并显示以下错误:

C:\Users\dbale\AndroidStudioProjects\MeetMe\app\src\main\AndroidManifest.xml 
Error:(2, 97) cvc-elt.1: Cannot find the declaration of element 'manifest'. 
Error:(1, 56) s4s-elt-schema-ns: The namespace of element 'x' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'. 
Error:(1, 56) s4s-elt-invalid: Element 'x' is not a valid element in a schema document. 
Error:(1, 56) schema_reference.4: Failed to read schema document 'null', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. 

这里是我的摇篮的应用程序文件:

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.1" 

defaultConfig { 
    applicationId "balevski.meetme" 
    minSdkVersion 7 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
compile 'com.android.support:appcompat-v7:23.0.1' 
compile 'com.android.support:support-v4:23.0.1' 
} 

我是新android的工作室,如果有人可以帮我弄清楚这个问题!

非常感谢。

+0

清理项目,并重建它 –

+0

我试图清理和重建。还是行不通。 – Borovez

+0

'我使用w3schools xml验证器验证了我的AndroidManifest.xml文件?是啊没有必要这样做,我并不感到惊讶它失败了,不用担心这个 – Blundell

回答

0

在棉花糖(Android 6.0/api 23)设备上,您现在需要在运行时请求权限,但不足以将其放入清单中。

http://developer.android.com/training/permissions/requesting.html

从文档:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { 

     // Show an explanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 
+0

谢谢,但是,你能为我提供一点解释吗?什么是变量“MY_PERMISSIONS_REQUEST_READ_CONTACTS”?我该如何设置。 - 另外,我可以不只是要求一次requestPermission,以便他们不必每次都要问他们想要获取联系信息吗? – Borovez

+0

阅读我链接你的网址。是的,你可以要求允许一次,他们可以说要记住这个选择 – Blundell

0

清除项目文件夹中的.idea文件并尝试重建项目。

+0

我试过了,并且什么也没有 – Borovez