Reputation:
For some reason, attaching an "internal" OnClickListener
to an ImageButton
crashes the app. For normal Button
s it works fine - it's only the ImageButton
that crashes it.
public class SomeWidgets extends Activity implements OnClickListener {
....
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
imagebutton = (ImageButton)findViewById(R.id.imagebutton)
imagebutton.setOnClickListener(this);
....
....
}
The above code crashes when clicking the imagebutton
. The button1
works fine, even though both are created and attached to the OnClickListener
in the exact same way. (It's the click that crashes it, and not the onClick
handling of the click.)
However, if I create a private inner OnClickListener
class and attach THAT one to the imagebutton, then it works fine.
Any ideas? Apparently, something goes wrong when attaching the "internal" (this)
OnClickListener
to an ImageButton
, while it works fine when attaching it to a Button
.
EDIT: Looks like I found the problem. If I override the onClick like this:
public void onClick(View v) {
if (((Button)v).getText().equals("A button")) {
edittext2.setText(edittext1.getText());
edittext1.setText("");
}
if (imagebutton.getId() == R.id.imagebutton) {
Toast.makeText(SomeWidgets.this, "This is an image button.",
Toast.LENGTH_LONG).show();
}
}
it crashes. However, if I remove the first if{} and only have the imagebutton if there, it works. This is obviously a silly error, but I'd appreciate and explanation as to why :-)
Upvotes: 0
Views: 602
Reputation:
Thanks, everyone who answered. I don't see how I could've missed using toString(). Somehow I got caught up with the ImageButton and thought that was the problem. Fixing the text check for the Button in the onClick() fixed everything.
Upvotes: 0
Reputation: 22291
please update below code:-
public void onClick(View v) {
if (v==button1) {
edittext2.setText(edittext1.getText());
edittext1.setText("");
} else if (v==imagebutton) {
Toast.makeText(SomeWidgets.this, "This is an image button.",
Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Reputation: 11227
the crash is caused by the conversion from imageButton to Button, try:
if (v instanceOf ImageButton) {
if (((ImagaeButton)v).getText().toString().equals("A button")) {
edit: if you add a OnClickListener to a Button and a ImageButon, you have to determine if the clicked object is the Button or ImageButton before you make a cast like (Button) v. So you have to do something like:
String ButtonText = "";
if (v instanceOf ImageButton) {
ButtonText = ((ImageButton) v).getText().toString();
}else if(v InstanceOf Button){
ButtonText = ((Button) v).getText().toString();
}
Upvotes: 0
Reputation: 35976
here change code
if (v.getId() == R.id.imagebutton) //imagebutton.getId() replace with View v
{
Toast.makeText(SomeWidgets.this, "This is an image button.",
Toast.LENGTH_LONG).show();
}
Upvotes: 0
Reputation: 2571
try with
if (((Button)v).getText().toString().equals("A button")) {
edittext2.setText(edittext1.getText().toString());
edittext1.setText("");
}
Upvotes: 1