android - ImageButton is Null -


i have imagebutton in fragment opened tablayout in main activity. when try reference imagebutton in main activity inside oncreate(bundle savedinstance):

 playpausebutton = (imagebutton)findviewbyid(r.id.play_pause); 

it returns null. fragment encloses imagebutton looks this:

 <imagebutton     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/play_pause"     android:layout_gravity="center_horizontal"     /> 

any ideas on why returns null?

you can't view inside fragment mainactivity directly not view included in it, included in fragment, have within fragment after has been created(i.e in onviewcreated() method) , instance of view created fragment:

 @override  public void onviewcreated(view view, bundle savedinstancestate) {      super.onviewcreated(view, savedinstancestate);       // declaring view want      imagebutton play_pause = (imagebutton) view.findviewbyid(r.id.play_pause);   } 

Comments