android - Null pointer exception when setting values to TextView in Fragments -
i have 2 fragments in mainactivity, first one, user clicks on button , send through listener char second fragment, depends of char textview in second fragment must print text. in metho onactivitycreated textview null.
this code: fragmentone
public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); btnchefcito = (button)getview().findviewbyid(r.id.btnchefcito); btnchefcita = (button)getview().findviewbyid(r.id.btnchefcita); btnchefcito.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { welcomelistener.elegirsexo(sexo); replacefragment(); } }); btnchefcita.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sexo="f"; welcomelistener.elegirsexo(sexo); replacefragment(); } }); }
this second fragment
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); tvibienvenida = (textview)getview().findviewbyid(r.id.tvibienvenida); } public void getsex(string sex){ if(sex.equals("m")){ tvibienvenida.settext(bienvenido); }else if(sex.equals("f")){ tvibienvenida.settext(bienvenida); } }
instead of doing in onactivitycreated
in oncreateview
:
private static view viewholder; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { viewholder = inflater.inflate(r.layout.yourfragmentlayout, container, false); btnchefcito = (button) viewholder.findviewbyid(r.id.yourbuttonid); btnchefcito.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { welcomelistener.elegirsexo(sexo); replacefragment(); } }); return viewholder; }
Comments
Post a Comment