如何在片段中使用 ContextCompat?它说片段无法转换为上下文

懒惰的头脑

我尝试在 Fragment 中使用ContextcompatActivityCompat但它显示

错误“错误:(49, 50) 错误:类型不兼容:CallFragment 无法转换为上下文”和“错误:(51, 51) 错误:类型不兼容:CallFragment 无法转换为活动”。

if(ContextCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED) { 
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL); 
}

我该如何解决这个问题?

艾哈迈德·卡拉姆
if(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL); } 

在片段中使用 getContext() 而不是 this

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章