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.
Your code is valid and works.
You just should remove
implements OnClickListenerif you don’t override the methodonClickdirectly in your activity.Add your .xml to your question because the problem looks to be here