Pre-Written android code throws nullPointerException -


i dont know why code throws nullpointerexception. did not written part of code , im pretty new staff. tried best not able find answer.

public class programexamactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); settitle("screen #1"); } public void onclick(view v) { switch(v.getid()) { case r.id.btn2: setcontentview(r.layout.screen2); settitle("screen #2"); break; } } 

first,

setcontentview(r.layout.screen2); settitle("screen #2"); 

you can't, shouldn't call more once.

and second if want use onclicklistener, need register widget , in code there widget.

in case class need implement view.onclicklistener if don' want work listeners anonymous classes

public class program... extends activity implements view.onclicklistener { ... } 

then need register this:

button btn = (button) findviewbyid(r.id.btn2); btn.setonclicklistener(this); 

note: if want start activity different title , content, have use intents , call startactivity().

more intents , there tutorial android: how switch between activities.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -