Reputation: 1726
Need help trying to figure out why the button onCLick event isn't working. I set it in onCreate but it doesn't seem to be working:
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Setup Refresh button listener.
Button btn = (Button) findViewById(R.id.btnRefresh);
btn.setOnClickListener(btnRefreshClick);
}
private OnClickListener btnRefreshClick = new OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Get Data from Server", Toast.LENGTH_LONG).show();
}
};
}
I even took out the Toast call and tried to write to LogCat but when I put a breakpoint on the Log.e statement, it never gets there.
Upvotes: 0
Views: 92
Reputation: 10726
Your code is valid and works.
You just should remove implements OnClickListener
if you don't override the method onClick
directly in your activity.
Add your .xml to your question because the problem looks to be here
Upvotes: 2
Reputation: 22240
You're specifying an OnClickListener
inside your onCreate()
method... this isn't right. If your activity is implementing OnClickListener, there should be a derived onClick() method.
Use:
btn.setOnClickListener(this);
instead and pop up the toast from the onClick() method separate from your onCreate().
Otherwise, you can try this instead:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Get Data from Server", Toast.LENGTH_LONG).show();
}
});
Technically, yours should work though, so make sure you're clicking the right button and that you don't have onClick
in your layout xml for the button.
Upvotes: 0
Reputation: 810
Are you sure you're pressing the right button? Are you sure you're resource id/layout is correct?
Upvotes: 2