button.setOnClickListener.错误 [英] button.setOnClickListener. error
本文介绍了button.setOnClickListener.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
这是 android 开发者
指南链接:http://developer.android.com/reference/android/widget/Button.html
这是编辑器显示代码的方式:
This is how the editor shows the code:
如您所见,setOnClickListener
在 android studio
中不起作用.有谁知道为什么?.
As you can see, setOnClickListener
does not work in android studio
. Does anyone know why?.
推荐答案
你的代码必须在一个方法中,比如 onCreate.
Your code has to be in a method, like onCreate.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
这篇关于button.setOnClickListener.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文