Skip to content Skip to sidebar Skip to footer

OnClickListener Doesnt Work In Tabbed Activity Android

The setOnclick Listener that I have written in the onCreate method doesn't work. The error is a null pointer exception error for button b1. I tried to initialize b1 before the on

Solution 1:

Put this before setting onClickListener

b1=(Button)rootView.findViewById(R.id.b1);

or set listener after you have assigned id. You should assign an id to a button before handling its click. Otherwise, how is android supposed to figure out what button is b1.


Solution 2:

That button is probably in your fragment's XML (R.layout.fragment_main) so you should add that onClickListener() in the onCreateView() of your PlaceholderFragment after b1=(Button)rootView.findViewById(R.id.b1);.

In any case you cant define an onClickListener() in an object that's not defined first. You first have to find that view usually by the id attribute from the XML with b1=(Button)rootView.findViewById(R.id.b1); or create one using a contructor, eg. b1= new Button(); (not suitable in your case).


Post a Comment for "OnClickListener Doesnt Work In Tabbed Activity Android"