Error At Android STUDIO -
i'm learning program android on android studio . when start application, following message appears on avd " unfortunalety , guitarstorev2 has stopped ."
can me ? (sorry if stupid mistake , not have familiarity language , , excuse error portuguese , because brazilian , not speak english fluently )
grateful attention mark tonial
public class mainactivity extends appcompatactivity { button btn_iniciaapp; public void inicia () { button btn_inciaapp = (button) findviewbyid(r.id.btn_iniciaapp); } // iniciando tela de produtos public void iniciaprod() { intent activityprod = new intent(this, activityprod.class); startactivity(activityprod); } // evento ao clicar no botão public void inicialistener() { this.btn_iniciaapp.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { mainactivity.this.iniciaprod(); } }); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(r.layout.activity_main); this.inicia(); this.inicialistener(); }
}
button btn_iniciaapp; public void inicia () { button btn_inciaapp = (button) findviewbyid(r.id.btn_iniciaapp); }
your program crashing nullpointerexception
. here, inicia
isn't setting value of class's btn_iniciaapp
: it's creating locally scoped button
shadowing class's btn_iniciaapp
. you're setting value of local button
, not class's.
since class's btn_iniciaapp
never set, remains null
, , call this.btn_iniciaapp.setonclicklistener
trigger nullpointerexception.