从Android应用程序调用并进行权限检查

时间:2016-09-08 06:38:48

标签: android exception-handling android-adapter classcastexception

我有一个带图像按钮的适配器。 OnClick ImageButton我希望拨打我正在解析的联系号码。这里要求我添加权限检查。我在这个阶段得到了阶级演员异常。请帮忙。

这是我的代码:

  holder.call.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ActivityCompat.checkSelfPermission(context, permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
           ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
              } else {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:"+storePro.getStoreContact()));
                    callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(callIntent);
                }

            }
        });

我得到的例外:

FATAL EXCEPTION: main 
java.lang.ClassCastException: com.example.project.app.AppController cannot be cast to android.app.Activity

2 个答案:

答案 0 :(得分:1)

粘贴此代码

 if (ContextCompat.checkSelfPermission(Activity.this,
                            Manifest.permission.CALL_PHONE)
                            != PackageManager.PERMISSION_GRANTED ) {


                        // Should we show an explanation?
                        if (ActivityCompat.shouldShowRequestPermissionRationale(Activity.this,
                                Manifest.permission.CALL_PHONE)) {

                            // 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.

                        } else {

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

                            ActivityCompat.requestPermissions(Activity.this,
                                    new String[]{Manifest.permission.CALL_PHONE},
                                    2);


                        }
                    }
                    else {
                       // do what you want

                   }

答案 1 :(得分:0)

FATAL EXCEPTION: main 
java.lang.ClassCastException: com.example.project.app.AppController cannot be cast to android.app.Activity

这意味着您已将错误的上下文值传递给适配器。您应该将活动上下文传递给它。