java Android setOnClickListener(this) 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9980068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android setOnClickListener(this) Error
提问by Grant
I have created a button in my Android application & I tried to set onclick listner to run onClick method like follows
我在我的 Android 应用程序中创建了一个按钮,我尝试将 onclick 监听器设置为运行 onClick 方法,如下所示
...
Button btn_ok;
btn_ok = (Button)findViewById(R.id.button1);
btn_ok.setOnClickListener(this);
}
public void onClick() {
EditText uN = (EditText) findViewById(R.id.EditText04);
uN.setText("Clicked!");
}
But Eclipse shows an error & says that "setOnClickListener" need to Cast Argument. After casting it is like this
但是 Eclipse 显示错误并说“setOnClickListener”需要转换参数。铸造后是这样的
btn_ok.setOnClickListener((OnClickListener) this);
Then when I'm running the program Emulator says that "Program has stopped unexpectedly"... How can I solve this problem ?
然后当我运行程序时,模拟器说“程序意外停止”......我该如何解决这个问题?
回答by Dmitry Zaytsev
Make sure that your class implements View.OnClickListener. You can`t just add onClick method, you must implement interface
确保您的类实现了 View.OnClickListener。你不能只添加onClick方法,你必须实现接口
回答by wsanville
The signature of your onClick
method is wrong, which leads me to believe you're not actually implementing the interface View.OnClickListener.
您的onClick
方法的签名是错误的,这让我相信您实际上并没有实现接口View.OnClickListener。
The signature should be:
签名应该是:
public void onClick(View v)
{
//your implementation, v is your button that was clicked
}
Note that the View that was clicked is passed in as an argument, so there's no need to call findViewById
from inside your onClick
method.
请注意,被单击的 View 作为参数传入,因此无需findViewById
从您的onClick
方法内部调用。
回答by Deva
implement the onClickListener from your activity and override the method:
从您的活动中实现 onClickListener 并覆盖该方法:
@override
public void onClick(View v)
{
switch(v.getId()){
case R.id.button1:
EditText uN = (EditText) findViewById(R.id.EditText04);
uN.setText("Clicked!");
break;
case default:
break;
}
}
Hope it helps.
希望能帮助到你。
回答by ρяσ?ρ?я K
setOnClickListener
take an OnClickListener
instance as parameter and OnClickListener
is an interface which content an onClick()
method and you are passing here setOnClickListener(this); current context. so you have two option either implements OnClickListener
in your activity and second use this way :
setOnClickListener
将一个OnClickListener
实例作为参数,它OnClickListener
是一个包含onClick()
方法的接口,您在这里传递 setOnClickListener(this); 当前上下文。所以你有两个选择,要么OnClickListener
在你的活动中实现,要么以这种方式使用:
this.btn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do your work here
}
});
回答by Aamir Abro
Make sure that you are implementing the interface View.OnClickListener and also pass View to onClick method
确保您正在实现接口 View.OnClickListener 并将 View 传递给 onClick 方法