不幸的是应用已经停止在真实设备上运行时

时间:2017-02-17 09:34:54

标签: android

当我在那时运行联系人列表项目时会发生。我不知道为什么会这样,请帮忙。

1 个答案:

答案 0 :(得分:0)

那是因为您使用的Android操作系统版本大于棒棒糖。

因此您必须为联系人添加权限

public void askForContactPermission(){     if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.M){         if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.READ_CONTACTS)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Contacts access needed");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setMessage("please confirm Contacts access");//TODO put real question
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @TargetApi(Build.VERSION_CODES.M)
                @Override
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(
                            new String[]
                                    {Manifest.permission.READ_CONTACTS}
                            , PERMISSION_REQUEST_CONTACT);
                }
            });
            builder.show();
            // Show an expanation 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.

        }