Android 按钮设置onclicklistener错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3545007/
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
Button setonclicklistener error
提问by chandu
I am having a problem right now with setOnClickListener
.
我现在遇到了问题setOnClickListener
。
When i put this following line:
当我把下面这行:
button.setOnClickListener(this);
And run the application then it does not run and shows a message that "Application closed forcefully".
并运行该应用程序,然后它不会运行并显示一条消息“应用程序已强制关闭”。
Could you please help me how I can set button onclick event in Android 2.2?
你能帮我如何在Android 2.2中设置按钮onclick事件吗?
回答by krunal shah
See if the code below works for you...
看看下面的代码是否适合你...
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show();
}
});
Remember to add });
at the end.
记得});
在最后加上。
回答by Paresh Mayani
For defining button click event in android, You can try the below code:
为了在android中定义按钮点击事件,您可以尝试以下代码:
public class Main_Activity extends Activity {
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.Button01);
myButton.setOnClickListener(new Button_Clicker());
}
class Button_Clicker implements Button.OnClickListener
{
@Override
public void onClick(View v) {
if(v==myButton)
{
Toast.makeText(v.getContext(), "Hello!! button Clicked", Toast.LENGTH_SHORT).show();
}
}
}
}
}
回答by apo
another possible reason ( happened to me ) is your activity must implement OnClickListener
另一个可能的原因(发生在我身上)是您的活动必须实施 OnClickListener
public class MainActivity extends Activity implements OnClickListener ...
回答by sami
Type View.onClickListener
instead of Button on ClickListener
键入View.onClickListener
而不是Button on ClickListener
回答by bool.dev
Although it's been a long time, thought it might help others who have this problem, it took me many trials to get it right. But i think what finally solved my problem was setting the clickable attribute of a button in the layout's xml to true.
Code sample:
虽然已经很长时间了,我认为这可能会帮助遇到此问题的其他人,但我花了很多时间才把它弄好。但我认为最终解决了我的问题的是将布局的 xml 中按钮的可点击属性设置为 true。
代码示例:
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:clickable="true">
</Button>
Further, if you would've looked at the DDMS perspective, you would've seen that the cause of the error was NullPointerException, which ofcourse was showing because clickable wasn't set. Correct me if i am wrong.
此外,如果您查看 DDMS 透视图,您会发现错误的原因是 NullPointerException,这当然是因为未设置可点击而显示的。如果我错了,请纠正我。
回答by Re MiDa
Check if in the class definition there is implements OnClickListener
检查类定义中是否有 implements OnClickListener