当前位置: 代码迷 >> Android >> android初学者sos
  详细解决方案

android初学者sos

热度:25   发布时间:2016-05-01 21:26:30.0
android菜鸟求助sos
Java code
package com.test;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class AT_16_CPtestActivity extends Activity {    /** Called when the activity is first created. */    private Button btngettxl = null;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btngettxl = (Button) findViewById(R.id.btngetlxr);        btngettxl.setOnClickListener(new getclick());    }    class getclick implements OnClickListener {        public void onClick(View v) {            ContentResolver contentResolver = getContentResolver();            // 获得所有的联系人            Cursor cursor = contentResolver.query(                    ContactsContract.Contacts.CONTENT_URI, null, null, null,                    null);            // 循环遍历            if (cursor.moveToFirst()) {                int idColumn = cursor                        .getColumnIndex(ContactsContract.Contacts._ID);                int displayNameColumn = cursor                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);                do {                    // 获得联系人的ID号                    String contactId = cursor.getString(idColumn);                    // 获得联系人姓名                    String disPlayName = cursor.getString(displayNameColumn);                    Toast.makeText(AT_16_CPtestActivity.this,                            "联系人姓名:" + disPlayName,                            Toast.LENGTH_LONG).show();                    // 查看该联系人有多少个电话号码。如果没有这返回值为0                    int phoneCount = cursor                            .getInt(cursor                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));                    if (phoneCount > 0) {                        // 获得联系人的电话号码列表                        Cursor phonesCursor = getContentResolver()                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                                        null,                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID                                                + " = " + contactId, null, null);                        if (phonesCursor.moveToFirst()) {                            do {                                // 遍历所有的电话号码                                String phoneNumber = phonesCursor                                        .getString(phonesCursor                                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                                Toast.makeText(AT_16_CPtestActivity.this,                                        "联系人电话:" + phoneNumber,                                        Toast.LENGTH_LONG).show();                            } while (phonesCursor.moveToNext());                        }                    }                } while (cursor.moveToNext());            }        }    }}

错误提示
Java code
04-25 07:30:48.399: ERROR/DatabaseUtils(8620): Writing exception to parcel04-25 07:30:48.399: ERROR/DatabaseUtils(8620): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=12794, uid=10034 requires android.permission.READ_CONTACTS04-25 07:30:48.399: ERROR/DatabaseUtils(8620):     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:295)04-25 07:30:48.399: ERROR/DatabaseUtils(8620):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:173)04-25 07:30:48.399: ERROR/DatabaseUtils(8620):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)04-25 07:30:48.399: ERROR/DatabaseUtils(8620):     at android.os.Binder.execTransact(Binder.java:320)04-25 07:30:48.399: ERROR/DatabaseUtils(8620):     at dalvik.system.NativeStart.run(Native Method)04-25 07:30:48.458: ERROR/AndroidRuntime(12794): FATAL EXCEPTION: main04-25 07:30:48.458: ERROR/AndroidRuntime(12794): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=12794, uid=10034 requires android.permission.READ_CONTACTS04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.os.Parcel.readException(Parcel.java:1322)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.content.ContentResolver.query(ContentResolver.java:262)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at com.test.AT_16_CPtestActivity$getclick.onClick(AT_16_CPtestActivity.java:31)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.view.View.performClick(View.java:2485)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.view.View$PerformClick.run(View.java:9080)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.os.Handler.handleCallback(Handler.java:587)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.os.Handler.dispatchMessage(Handler.java:92)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.os.Looper.loop(Looper.java:123)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at android.app.ActivityThread.main(ActivityThread.java:3683)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at java.lang.reflect.Method.invokeNative(Native Method)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at java.lang.reflect.Method.invoke(Method.java:507)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)04-25 07:30:48.458: ERROR/AndroidRuntime(12794):     at dalvik.system.NativeStart.main(Native Method)04-25 07:30:50.168: ERROR/InputDispatcher(61): channel '406e9038 com.test/com.test.AT_16_CPtestActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x804-25 07:30:50.168: ERROR/InputDispatcher(61): channel '406e9038 com.test/com.test.AT_16_CPtestActivity (server)' ~ Channel is unrecoverably broken and will be disposed!


------解决方案--------------------
加读写联系人的权限没有
  相关解决方案