自定义适配器类中的Marshmallow权限

时间:2016-11-29 06:33:50

标签: android android-recyclerview android-permissions

尝试在RecyclerView项目上拨打电话时获取此异常点击...

FATAL EXCEPTION: main 
    Process: com.itec.kdaportal, PID: 11701
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
    at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:97)
    at android.app.Activity.shouldShowRequestPermissionRationale(Activity.java:4229)
    at android.support.v4.app.ActivityCompatApi23.shouldShowRequestPermissionRationale(ActivityCompatApi23.java:50)
    at android.support.v4.app.ActivityCompat.shouldShowRequestPermissionRationale(ActivityCompat.java:419)
    at com.itec.kdaportal.Adapters.DirCustomAdapter$ViewHolder$1.onClick(DirCustomAdapter.java:65)
    at android.view.View.performClick(View.java:5716)
    at android.view.View$PerformClick.run(View.java:22596)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7331)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我认为导致这种兴奋的代码就在这里......

// Provide a suitable constructor (depends on the kind of dataset)
public DirCustomAdapter(Context context, Activity activity, ArrayList<Directories> dir) {
    mContext = context;
    mActivity = activity;
    this.list = dir;
}

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    Directories dirObj;
    // each data item is just a string in this case
    public TextView org, contact;
    private int PHONE_PERMISSION_CODE = 03;

    public ViewHolder(View v) {
        super(v);
        org = (TextView) v.findViewById(R.id.tvOrganization);
        contact = (TextView) v.findViewById(R.id.tvContactNo);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, dirObj.getOrgName() + " clicked!", Toast.LENGTH_SHORT).show();
                phoneNo = dirObj.getContactNo();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if(ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED){
                        mContext.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + dirObj.getContactNo())));
                    }else{
                        if(ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CALL_PHONE)){
                            Toast.makeText(mContext, "App requires Phone Call permission.\nPlease allow that in the device settings.", Toast.LENGTH_LONG).show();
                        }
                        ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CALL_PHONE}, PHONE_PERMISSION_CODE);
                    }
                }else{
                    mContext.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + dirObj.getContactNo())));
                }
            }
        });
    }
    public void bindData(Directories c){
        dirObj = c;
        org.setText(c.getOrgName());
        contact.setText(c.getContactNo());
    }
}

以这种方式创建CustomAdapter对象......

   dirAdapter = new DirCustomAdapter(this, new DirectoryActivity(), arrayList);
    mRecyclerView.setAdapter(dirAdapter);

App在Marshmallow之前的设备上工作正常。

请帮我修理它。

还建议请问,有没有其他方法可以通过点击RecyclerView项目点击来询问Marshmallow权限?

谢谢和问候

1 个答案:

答案 0 :(得分:0)

创建Activity类的新对象是错误的方法。

dirAdapter = new DirCustomAdapter(this, new DirectoryActivity(), arrayList); // HERE new DirectoryActivity() is incorrect way to pass Activity reference.
 mRecyclerView.setAdapter(dirAdapter);

使用以下代码

dirAdapter = new DirCustomAdapter(this, DirectoryActivity.this, arrayList);
mRecyclerView.setAdapter(dirAdapter);

我在此假设您正在调用DirectoryActivity.java Activity类的代码段。