I create a button from layout and it works fine(display on screen when I test). When I add the click event for this Button
, I always receive a NullPointerException
(you can see in the below code):
Button b1;
int REQUEST_CODE =1;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
b1 = (Button) findViewById(R.id.web_button);
b1.setOnClickListener(new OnClickListener() { //ERROR THIS LINE
public void onClick(View arg0){}
});
setContentView(R.layout.intent);
}
You are searching for the Button
before you set the content view. You'll have to do this:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
b1 = (Button) findViewById(R.id.web_button);
b1.setOnClickListener(new OnClickListener() { //ERROR THIS LINE
public void onClick(View arg0){
}
});
}