访问android marshmallow中的联系人的问题

时间:2016-06-13 09:51:18

标签: android android-contacts android-6.0-marshmallow android-permissions

我在应用程序中从手机访问联系人时遇到错误。 我添加了android marshmallow的访问权限。当我接受许可时,应用程序崩溃。而且我不确定我使用过的权限代码......有人可以告诉我哪里出错了......代码就是......

 public class ContactsDisplay extends AppCompatActivity implements  OnItemClickListener,FragmentDrawer.FragmentDrawerListener {

        private static String TAG = ContactsDisplay.class.getSimpleName();

        private Toolbar mToolbar;
        private FragmentDrawer drawerFragment;

        Cursor phones;

        List<String> name1 = new ArrayList<String>();
        List<String> phno1 = new ArrayList<String>();
        MyAdapter ma ;
        Button select;

        private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //getActionBar().setDisplayShowTitleEnabled(false);
            setContentView(R.layout.contacts_display);
            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            ImageView img = (ImageView)findViewById(R.id.imageView10);
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayShowHomeEnabled(true);

            drawerFragment = (FragmentDrawer)
                    getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
            drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
            drawerFragment.setDrawerListener(this);

            // display the second navigation drawer view on app launch
            displayView(0);

            getAllContacts(this.getContentResolver());
            ListView lv= (ListView) findViewById(R.id.lv);
            ma = new MyAdapter();
            lv.setAdapter(ma);
            lv.setOnItemClickListener(this);
            lv.setItemsCanFocus(false);
            lv.setTextFilterEnabled(true);
            // adding
            select = (Button) findViewById(R.id.button1);

            select.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    StringBuilder checkedcontacts= new StringBuilder();
                    System.out.println(".............."+ma.mCheckStates.size());
                    for(int i = 0; i < name1.size(); i++)

                    {
                        if(ma.mCheckStates.get(i)==true)
                        {
                            checkedcontacts.append(name1.get(i).toString());
                            checkedcontacts.append("\n");

                        }
                        else
                        {
                            System.out.println("Not Checked......"+name1.get(i).toString());
                        }


                    }

                    Toast.makeText(ContactsDisplay.this, checkedcontacts,Toast.LENGTH_LONG).show();
                }
            });

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

                ma.toggle(position);
            }




        public  void getAllContacts(ContentResolver cr) {

            int result = ContextCompat.checkSelfPermission(ContactsDisplay.this, Manifest.permission.READ_CONTACTS);
            if (result == PackageManager.PERMISSION_GRANTED){

                phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                contactsdisplay();

            } else {

                requestForLocationPermission();
            }
        }

        private void requestForLocationPermission()
        {

            if (ActivityCompat.shouldShowRequestPermissionRationale(ContactsDisplay.this, Manifest.permission.READ_CONTACTS))
            {
            }
            else {

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

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
        {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    {
  getAllContacts(ContactsDisplay.this.getContentResolver());                        
contactsdisplay();
                    }
                    break;
            }
        }

        public  void contactsdisplay() {

            phones.moveToFirst();
        do
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println(".................."+phoneNumber);
            name1.add(name);
            phno1.add(phoneNumber);
        }while (phones.moveToNext());
        }

        class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
        {  private SparseBooleanArray mCheckStates;
            LayoutInflater mInflater;
            TextView tv1,tv;
            CheckBox cb;
            MyAdapter()
            {
                mCheckStates = new SparseBooleanArray(name1.size());
                mInflater = (LayoutInflater)ContactsDisplay.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return name1.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub

                return 0;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View vi=convertView;
                if(convertView==null)
                    vi = mInflater.inflate(R.layout.row, null);
                TextView tv= (TextView) vi.findViewById(R.id.textView1);
                tv1= (TextView) vi.findViewById(R.id.textView2);
                cb = (CheckBox) vi.findViewById(R.id.checkBox_id);
                tv.setText("Name :"+ name1.get(position));
                tv1.setText("Phone No :"+ phno1.get(position));
                cb.setTag(position);
                cb.setChecked(mCheckStates.get(position, false));
                cb.setOnCheckedChangeListener(this);

                return vi;
            }
            public boolean isChecked(int position) {
                return mCheckStates.get(position, false);
            }

            public void setChecked(int position, boolean isChecked) {
                mCheckStates.put(position, isChecked);
                System.out.println("hello...........");
                notifyDataSetChanged();
            }

            public void toggle(int position) {
                setChecked(position, !isChecked(position));
            }
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub

                mCheckStates.put((Integer) buttonView.getTag(), isChecked);
            }
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            if(id == R.id.action_search){
                 /*Toast.makeText(getApplicationContext(), vault_no, Toast.LENGTH_SHORT).show();
                return true;*/
                logout();
            }

            if(id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        private void logout(){
            //Creating an alert dialog to confirm logout
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setMessage("Are you sure you want to logout?");
            alertDialogBuilder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {

                            //Getting out sharedpreferences
                            SharedPreferences preferences = getSharedPreferences(ClickToLogin.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                            //Getting editor
                            SharedPreferences.Editor editor = preferences.edit();

                            //Puting the value false for loggedin
                            editor.putBoolean(ClickToLogin.LOGGEDIN_SHARED_PREF, false);

                            //Putting blank value to email
                            editor.putString(ProfileLogin.EMAIL_SHARED_PREF, "");
                            editor.putString(ClickToLogin.FULLNAME_SHARED_PREF,  "");
                            editor.putString(ClickToLogin.MOBILE_SHARED_PREF, "");
                            editor.putString(ClickToLogin.POB_SHARED_PREF,  "");
                            editor.putString(ClickToLogin.DOB_SHARED_PREF,  "");
                            editor.putString(ClickToLogin.PIN_SHARED_PREF, "");
                            editor.putString(ClickToLogin.EMAIL_SHARED_PREF,  "");
                            editor.putString(ClickToLogin.DOC_SHARED_PREF, "");
                            editor.putString(ClickToLogin.YEAR_SHARED_PREF, "");
                            editor.clear();
                            //Saving the sharedpreferences
                            editor.commit();

                            //Starting login activity
                            Intent intent = new Intent(ContactsDisplay.this, ProfileLogin.class);
                            startActivity(intent);
                        }
                    });

            alertDialogBuilder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {

                        }
                    });

            //Showing the alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();

        }

        @Override
        public void onDrawerItemSelected(View view, int position) {
            displayView(position);
        }

        private void displayView(int position) {
            Fragment fragment = null;
            String title = getString(R.string.app_name);
            switch (position) {
                case 0:
                    /*fragment = new HomeFragment();*/
                    title = "Work Details1";

                    break;

                case 1:
                    /*fragment = new ProfileFragment();
                    title = getString(R.string.title_profile);
                    break;*/
                    startActivity(new Intent(this, ContactsDisplay.class));
                    return;
                case 2:

                    startActivity(new Intent (this,Events.class));
                    return;
                    /*fragment = new AboutFragment();
                    title = getString(R.string.title_about);
                    break;*/
                case 3:
                    fragment = new MessagesFragment();
                    title = getString(R.string.title_messages);
                    break;

                case 4:
                    fragment = new VideoFragment();
                    title = getString(R.string.title_video);
                    break;
                case 5:
                    fragment = new GalleryFragment();
                    title = getString(R.string.title_gallery);
                    break;
                case 6:
                    fragment = new GalleryFragment();
                    title = "Gallery";
                    break;

                case 7:
                    fragment = new EventsFragment();
                    title = getString(R.string.title_events);
                    break;

                default:
                    break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.container_body, fragment);
                fragmentTransaction.commit();

                // set the toolbar title
                getSupportActionBar().setTitle(title);
            }
        }

        @Override
        public void onBackPressed() {

            finish();
            Intent intent = new Intent(ContactsDisplay.this, DocUpload.class);

            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);

        }

    }

这里是logcat

06-13 05:37:22.000 11581-11581/com.example.miisky E/AndroidRuntime: FATAL EXCEPTION: main
                                                          Process: com.example.miisky, PID: 11581
                                                          java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.miisky/com.example.miisky.ContactsDisplay}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToNext()' on a null object reference
                                                          at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                          at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                          at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                          at android.os.Looper.loop(Looper.java:148)
                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                        Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToNext()' on a null object reference
                                                          at com.example.miisky.ContactsDisplay.contactsdisplay(ContactsDisplay.java:199)
                                                          at com.example.miisky.ContactsDisplay.onRequestPermissionsResult(ContactsDisplay.java:190)
                                                          at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
                                                          at android.app.Activity.dispatchActivityResult(Activity.java:6432)
                                                          at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                          at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                         at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:148) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2 个答案:

答案 0 :(得分:1)

您正在调用已检查所需权限的getAllContacts(),如果已授予权限,则您将获得联系人列表。您的代码问题是,当您没有权限时,您要求它。所以现在当用户允许或拒绝时,你再次需要从onRequestPermissionsResult()调用相同的方法getAllContacts()。目前正在发生用户允许,但您还没有取出联系人并直接阅读它。

在onRequestPermissionsResult()中添加你的getAllContacts()调用,即

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                     getAllContacts(ContactsDisplay.this.getContentResolver());
                    contactsdisplay();
                }else{
                   //Show a dialog with explanation why you want contacts permission and ask for permission again or else simply display appropriate error or alert message.
                }
                break;
        }
    }

因此,首先要解释当你调用getAllContacts()并且你已经获得了权限时,你的Cursor手机将拥有值。但是,如果您没有权限,那么您将显示一个请求权限的对话框,当用户允许您直接调用contactsdisplay()时,这是错误的。你必须首先获得getAllContacts()作为刚刚授予你权限的用户,然后调用contactsdisplay()。

答案 1 :(得分:0)

我的代码的问题是AppcompatActivity..i将AppcompatActivity更改为Actitvity ..现在代码运行良好..即使访问联系人时要求访问marshmallow的权限..

所以这是最终的代码..

public class DisplayContact extends Activity implements OnItemClickListener{

    //ArrayList to store name and phone number
    List<String> name1 = new ArrayList<String>();
    List<String> phno1 = new ArrayList<String>();
    MyAdapter ma ;
    Button select;
    Cursor phones;

    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayShowTitleEnabled(false);
        setContentView(R.layout.display);


        getAllContacts(this.getContentResolver());
        ListView lv= (ListView) findViewById(R.id.lv);
        ma = new MyAdapter();
        lv.setAdapter(ma);
        lv.setOnItemClickListener(this);
        lv.setItemsCanFocus(false);
        lv.setTextFilterEnabled(true);
        // adding
        select = (Button) findViewById(R.id.button1);
        select.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                StringBuilder checkedcontacts= new StringBuilder();

                for(int i = 0; i < name1.size(); i++)

                {
                    if(ma.mCheckStates.get(i)==true)
                    {
                        checkedcontacts.append(name1.get(i).toString());
                        checkedcontacts.append(phno1.get(i).toString());
                        checkedcontacts.append("\n");

                    else
                    {

                    }
                }
               Toast.makeText(DisplayContact.this, checkedcontacts, Toast.LENGTH_LONG).show();

            }
        });

    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        ma.toggle(arg2);
    }

    public  void getAllContacts(ContentResolver cr) {

        int result = ContextCompat.checkSelfPermission(DisplayContact.this, Manifest.permission.READ_CONTACTS);
        if (result == PackageManager.PERMISSION_GRANTED){

            phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            contactsdisplay();

        } else {

            requestForLocationPermission();
        }
    }

    private void requestForLocationPermission()
    {

        if (ActivityCompat.shouldShowRequestPermissionRationale(DisplayContact.this, Manifest.permission.READ_CONTACTS))
        {
        }
        else {

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

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    getAllContacts(DisplayContact.this.getContentResolver());
                    contactsdisplay();
                }
                break;
        }
    }

    public  void contactsdisplay() {

        //Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println(".................."+name+ "" +phoneNumber);
            name1.add(name);
            phno1.add(phoneNumber);
        }

        phones.close();
    }

    class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
    {  private SparseBooleanArray mCheckStates;
        LayoutInflater mInflater;
        TextView tv1,tv;
        CheckBox cb;
        MyAdapter()
        {
            mCheckStates = new SparseBooleanArray(name1.size());
            mInflater = (LayoutInflater)DisplayContact.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return name1.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub

            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View vi=convertView;
            if(convertView==null)
                vi = mInflater.inflate(R.layout.row, null);
            tv= (TextView) vi.findViewById(R.id.contact_name);
            tv1= (TextView) vi.findViewById(R.id.phone_number);
            cb = (CheckBox) vi.findViewById(R.id.checkBox_id);
            tv.setText("Name :"+ name1.get(position));
            tv1.setText("Phone No :"+ phno1.get(position));
            cb.setTag(position);
            cb.setChecked(mCheckStates.get(position, false));
            cb.setOnCheckedChangeListener(this);

            return vi;
        }
        public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);
        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            // TODO Auto-generated method stub

            mCheckStates.put((Integer) buttonView.getTag(), isChecked);
        }
    }

    @Override
    public void onBackPressed() {

        finish();
        Intent intent = new Intent(DisplayContact.this, ProfileDisplay.class);

        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }
}