I have placed the following code in the main activity in the onCreate method
public class MyTest extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//... some other code here to init the layout
Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And have placed the following code outside the class MyTest
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
break;
case R.id.button2:
break;
}
}
When running the application it crashes. Please can someone help me
Actually you forgot
setContentView(R.layout.<main_xml>);inonCreate()of Activity before definingButtons.Something like,
Update:
Also You have to put
at outside of
onCreate()method as part of yourMyTest Activity Class. Not the outside ofMyTest Activity Class.