android - Unable to set relativelayout to invisible when activity starts gives NullPointerException -
i have layout have switch , relativelayout contains 2 image buttons. default switch remains in off state. want when activity loads want set relative layout b invisible , if switch set in on state relativelayout should visible.
when try set invisible in oncreate giving me nullpointerexception..
how can set layout b invisible when activity starts , if switch checked layout should visible.. ? please help..
layout.xml :-
<switch android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/btnstylebeige" android:id="@+id/switch1" android:layout_marginright="10dp" android:layout_aligntop="@+id/textview7" android:layout_alignstart="@+id/button5" /> <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/relative2" android:layout_margintop="40dp" android:layout_below="@+id/switch1" android:layout_alignparentstart="true" android:layout_alignend="@+id/taskname"> <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pickdate" android:src="@drawable/c2" style="@style/btnstylebeige" android:layout_marginstart="65dp" android:layout_alignparenttop="true" android:layout_alignparentstart="true" /> <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/picktime" style="@style/btnstylebeige" android:src="@drawable/c1" android:layout_marginend="65dp" android:layout_alignparenttop="true" android:layout_alignparentend="true" /> </relativelayout>
activity :-
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.addtask); getactionbar().setdisplayhomeasupenabled(true); relativelayout2.setvisibility(view.invisible); switch aswitch = (switch) findviewbyid(r.id.switch1); aswitch.setoncheckedchangelistener(this); init(); /** capture our view elements */ //pdisplaydate = (textview) findviewbyid(r.id.displaydate); ppickdate = (imagebutton) findviewbyid(r.id.pickdate); /** listener click event of button */ ppickdate.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(date_dialog_id); } }); /** capture our view elements */ //pdisplaytime = (textview) findviewbyid(r.id.displaytime); ppicktime = (imagebutton) findviewbyid(r.id.picktime); /** listener click event of button */ ppicktime.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(time_dialog_id); } }); /** current date , time */ final calendar cal = calendar.getinstance(); pyear = cal.get(calendar.year); pmonth = cal.get(calendar.month); pday = cal.get(calendar.day_of_month); mhour = cal.get(calendar.hour_of_day); mminute = cal.get(calendar.minute); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { relativelayout2 = (relativelayout)findviewbyid(r.id.relative2); if(ischecked) { relativelayout2.setvisibility(view.gone); } else { relativelayout2.setvisibility(view.visible); } }
you did not created reference of relative layout.
before line..
relativelayout2.setvisibility(view.invisible);
add 1
relativelayout relativelayout2 = (relativelayout) findviewbyid(r.id.relative2);
Comments
Post a Comment