Q:

OnClickListener crashing application

The following code is crashing my application on startup.
It compiles just fine but just crashes.
Without this code it runs perfectly.
This is the code
private OnClickListener listentest = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

    public void onCreate(Bundle savedInstanceState) {
    Button teamsbtn = (Button)findViewById(R.id.teams);
  //  teamsbtn.setOnClickListener(listentest); 
     }

Thanks,
Andrew

A:

Interesting.  It could be something to do with "when" the OnClickListener is created in the overall app lifecycle, with regard to parent class in particular.  I haven't personally tried it like this.  
What if you instead make a class which you instantiate during the onCreate ?
public void onCreate(Bundle savedInstanceState) {
    listentest = new MyOnClickListener();

    Button teamsbtn = (Button)findViewById(R.id.teams);

    teamsbtn.setOnClickListener(listentest); 
 }

private class MyOnClickListener implements OnClickListener {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

