【问题标题】:incompatible types: cannot be converted to Context不兼容的类型:无法转换为 Context
【发布时间】:2017-05-20 18:27:16
【问题描述】:

我在尝试修改代码以处理 Android Nougat 中的文档时遇到错误。

不兼容的类型:无法转换为上下文

这是我的代码

    documentViewHolder.preview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    File document = new File(Environment.getExternalStorageDirectory() + "/heyJudeDocuments/" + getItem(position).attachment_id);  // -> filename = maven.pdf 
                    Uri path = FileProvider.getUriForFile(MessageAdapter.this,BuildConfig.APPLICATION_ID + ".provider",document);
                    Intent docIntent = new Intent(Intent.ACTION_VIEW);
                    docIntent.setDataAndType(path, getItem(position).mime);
                    docIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    docIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

MessageAdapter.this 部分似乎是错误的。有人能指出我哪里出错了吗?

【问题讨论】:

    标签: android android-7.1-nougat


    【解决方案1】:
    public class MessageAdapter extends BaseAdapter {
    private Context context;
    
    public MessageAdapter(Context context) {
        this.context = context;
    }
    

    在 Uri 中使用该上下文(而不是 MessageAdapter.this)

     Uri path = FileProvider.getUriForFile(context ,BuildConfig.APPLICATION_ID + ".provider",document);
    

    【讨论】:

      【解决方案2】:

      MessageAdapter 不是Context 的子类。通常,对于您正在做的事情(大概是开始一项活动),您可以访问您所在的Activity,这就是在这里使用的Context

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-03
        • 1970-01-01
        • 2021-12-20
        • 2016-10-17
        • 1970-01-01
        • 2017-03-29
        相关资源
        最近更新 更多