按钮OnclickListener内的烤面包不起作用

编程入门 行业动态 更新时间:2024-10-26 06:29:03
本文介绍了按钮OnclickListener内的烤面包不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

intdelay 在代码开头由 1000 初始化。 ,我正在尝试通过单击按钮添加祝酒词来确保 intdelay 的值由编辑框中的值更新。 ,但是运行应用程序时出现以下问题:

intdelay was initialized at the beginning of the code by 1000. and I am trying to make sure that intdelay's value is updated by the value in the edit box by adding a toast when the button is clicked. but when I run the app, I get the following problem:

Error:(46, 22) error: no suitable method found for makeText(<anonymous OnClickListener>,String,int) method Toast.makeText(Context,CharSequence,int) is not applicable (argument mismatch; <anonymous OnClickListener> cannot be converted to Context) method Toast.makeText(Context,int,int) is not applicable (argument mismatch; <anonymous OnClickListener> cannot be converted to Context)

这是我的代码的一部分:

This is the part of my code:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); delayedttxt=(EditText)findViewById(R.id.delayedttxt); String delay=delayedttxt.getText().toString(); //this will get a string try{ int intdelay = Integer.parseInt(delay); }catch(NumberFormatException ex){ // handle your exception } Button btn=(Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { flashLight.switchFlash(); Toast.makeText(this,"your integer is " + intdelay , Toast.LENGTH_LONG).show(); } });

我该如何解决?

推荐答案

这是因为 makeText 希望将 Context 作为第一个参数。您位于 OnClickListener 的 onClick 函数中。这意味着此指向您的 OnClickListener 。 您必须有这样的东西

That's because makeText wants to have a Context as a first argument. You are inside the onClick function of an OnClickListener. This means that this points to your OnClickListener. You must have something like this

Toast.makeText(YourActivity.this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();

更多推荐

按钮OnclickListener内的烤面包不起作用

本文发布于:2023-10-16 11:44:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   不起作用   烤面包   OnclickListener

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!