简体   繁体   English

onClickListner无法传递“ this”参数错误

[英]Can't get pass “this” argument Error with the onClickListner

What am I doing wrong here? 我在这里做错了什么? I can't get pass "this" argument Error with the onClickListner. 我无法通过onClickListner传递“ this”参数错误。

 public void onClick(View V){
            txt.setText("Button is Clicked");

            Intent i = new Intent(Intent.ACTION_CALL);
            String encodedHash = Uri.encode("#");
            i.setData(Uri.parse("tel:"+"*804"+encodedHash));

            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
            {
                return;
            }

            startActivity(i);
        }

Please implement View.OnClickListener because you must implement this interface alongside onClick() method. 请实现View.OnClickListener因为您必须与onClick()方法一起实现此接口。 Or you can try this code below: 或者您可以在下面尝试以下代码:

public Button btnClick;
btnClick=view.findViewById(R.id.btn_click);

btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
  txt.setText("Button is Clicked");

        Intent i = new Intent(Intent.ACTION_CALL);
        String encodedHash = Uri.encode("#");
        i.setData(Uri.parse("tel:"+"*804"+encodedHash));

        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
        {
            return;
        }

        startActivity(i);
        }
    });

Your question doesn't provide enough information but I think you are getting an error about passing an OnClickListener instead of an Activity because you're defining an inner anonymous class so this refers to this anonymous class. 您的问题未提供足够的信息,但我认为您在传递OnClickListener而不是Activity遇到错误,因为您正在定义内部匿名类,因此this引用此匿名类。 If want to access your outer/container class you should call it like: OuterClass.this or ( this@OuterClass in kotlin): 如果想访问你的外部/容器类,你应该称呼它: OuterClass.this或( this@OuterClass在科特林):

public void onClick(View V){ txt.setText("Button is Clicked");

        Intent i = new Intent(Intent.ACTION_CALL);
        String encodedHash = Uri.encode("#");
        i.setData(Uri.parse("tel:"+"*804"+encodedHash));

        if (ActivityCompat.checkSelfPermission(MyActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
        {
            return;
        }

        startActivity(i);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Android 错误:无法通过 null 子项中的参数“pathString” - Firebase Android error: Can't pass null for argument 'pathString' in child Firebase 错误无法为 child() 中的参数“pathString”传递 null - Firebase error Can't pass null for argument 'pathString' in child() 无法在child()Firebase错误中为参数'pathString'传递null - Can't pass null for argument 'pathString' in child() firebase error 无法使用自定义适配器从 firebase 获取数据,“Сan't pass null for argument pathString in child ()” - Can't get data from firebase using custom adapter, “Сan't pass null for argument pathString in child ()” 无法为child()中的参数“ pathString”传递null - Can't pass null for argument 'pathString' in child() 从Firebase填充Textview。 错误:在child()中无法为参数'pathString'传递null - Populate Textview from Firebase. Error: Can't pass null for argument 'pathString' in child() java.lang.NullPointerException:无法为child()中的参数'pathString'传递null-MessagesDbRef中发生错误 - java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() - Error Occurs in MessagesDbRef 不能为 child() 中的参数“pathString”传递 null。 当我尝试更新数据时,我只是遇到了这个错误 - Can't pass null for argument 'pathString' in child(). I just face this error when I was trying to update the data 不能为 child() 中的参数“pathString”传递 null? 这是我从 firebase 检索数据时遇到的错误 - Can't pass null for argument 'pathString' in child()? This the error I'm getting when retrieving data from firebase 将OnClickListner添加到CardView并获取数据 - Add OnClickListner to CardView and get data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM