java - Android: ImageButton causes crash upon assigning it a listener -
so, basically, have imagebutton called boutonportail, , called logo. initializing boutonportail works fine, when assign listener boutonportail.setonclicklistener(boutonportaillistener); app crashes, , don't know why @ all. it's not problem listener since when assign logo works fine. doubt problem xml since copy/pasted code of logo.
my app prompts password, , if password correct switches view logo , boutonportail. note password prompt view has logo.
is problem boutonportail not on main view? tried assigning listener after switching views, still crashes.
edit: after putting button in main view, problem button not in main view, when put in main view works fine. why crashes though?
also, reason can't manage change image of button boutonportail.setimageresource(r.drawable.boutonfermer);. (this doesn't happen when in main view)
oncreate method:
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); logo = (imagebutton)findviewbyid(r.id.logo); boutonportail = (imagebutton)findviewbyid(r.id.boutonportail); codeentered = (edittext)findviewbyid(r.id.codeentered); codesurnotice = (textview)findviewbyid(r.id.codesurnotice); //attribute listeners logo.setonclicklistener(boutonportaillistener); codeentered.addtextchangedlistener(textwatcher); codeentered.setonkeylistener(codeenteredlistener); method change view:
void codecorrect() { setcontentview(r.layout.activity_readytopress); boutonportail.setonclicklistener(boutonportaillistener); //this line crashes app, if put in oncreate } listener:
private onclicklistener boutonportaillistener = new onclicklistener() { @override public void onclick(view v) { boutonstate++; if(boutonstate>=4) boutonstate=0; boutonportail.setimageresource(r.drawable.boutonfermer); } }; xml:
<imagebutton android:id="@+id/boutonportail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="40dip" android:src="@drawable/boutonouvrir" android:background="#00000000" style="#00000000" android:layout_gravity="center" /> here's full code if want test (note you'll have call codecorrect() method manually since don't have access bluetooth device use):
mainactivity.java http://pastebin.com/zxdahpz6 activity_main.xml http://pastebin.com/f14cvbkj activity_readytopress.xml http://pastebin.com/0izm91eq boutonouvrir.png http://puu.sh/mlgeu.png ouvertureencours.png http://puu.sh/mlgfi.png boutonfermer.png http://puu.sh/mlge5.png fermetureencours.png http://puu.sh/mlggw.png
thanks :)
you not have button id boutonportail in activity_main.xml
it crashing null pointer exception. cannot add listener null object.
here relevant code:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // on récupère toutes les vues dont on besoin logo = (imagebutton)findviewbyid(r.id.logo); boutonportail = (imagebutton)findviewbyid(r.id.boutonportail); so call setcontentview(r.layout.activity_main) , call findviewbyid(r.id.boutonportail)
the findcontentview() return null, because acitivty_main.xml not have view id of value. boutonportail null.
then call boutonportail.setonclicklistener(boutonportaillistener) crash null pointer exception because boutonportail null
Comments
Post a Comment