java - android : Is it possible to inherit from MainActivity class? -
i started learning java , android development
week ago , have couple of basic things i'd want know.
the functioning of app has 10 buttons , each of them disabled after has been clicked once. if total of 5 buttons have been clicked, buttons must disabled. didn't use button array, want know why doesn't work. (all necessary imports have been made)
the
mainactivity
class goes this:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); starttext(); } class1 c1object = new class1(); textview textobj; button button1,button2,button3,button4 /* other 6 buttons*/ ; public void starttext(){ textobj = (textview)findviewbyid(r.id.textview); textobj.settext("no button has been clicked"); } public void click1(view view) { button1 = (button)findviewbyid(r.id.button1); c1object.disablebutton(button1,textobj,1); } public void click2(view view) { button2 = (button)findviewbyid(r.id.button2); c1object.disablebutton(button2,textobj,2); } public void click3(view view) { button3 = (button)findviewbyid(r.id.button3); c1object.disablebutton(button3,textobj,3); } public void click4(view view) { button4 = (button)findviewbyid(r.id.button4); c1object.disablebutton(button4,textobj,4); } // 6 more buttons }
class1
this:
public class class1 { class2 c2object = new class2(); private int clickcount=0; public void disablebutton(button b, textview t, int i) { b.setenabled(false); string str = integer.tostring(i); t.settext("you clicked button: " + str); clickcount++; if(clickcount==5) c2object.disableall(); } }
and finally,
class2
goes this:
public class class2 { mainactivity mainobject = new mainactivity(); public void disableall(){ mainobject.button1.setenabled(false); mainobject.button2.setenabled(false); mainobject.button3.setenabled(false); mainobject.button4.setenabled(false); //similarly other 6 buttons } }
if run app launches white screen shown , app closes.
after several tries, realized happens when created object of mainactivity
class. tried inheriting mainactivity
result same.
i tried changing class2
this:
public class class2 { public void disableall(){ button b1 = (button)findviewbyid(r.id.button1); b1.setenabled(false); //similarly other 9 buttons } }
i noticed can't use findviewbyid
here.
these things i'd know:
can inherit or create object of
mainactivity
?why can't use
findviewbyid
class apartmainactivity
?what do disable buttons after
clickcount==5
?you've seen way of coding. practices need follow? other suggestions make way of coding better?
thank in advance!
you cannot instantiate activity
class via constructor. wont able write mainactivity mainobject = new mainactivity()
. still wouldn't want if possible, creating new instance of mainactivity
, whereas want conduct operations on buttons in existing mainactivity
.
one way around supply mainactivity
argument neither method or constructor. i.e.
class classtwo { private mainactivity activity; classtwo(mainactivity activity) { this.activity = activity; } void disableall() { activity.btn1.setenabled(false); //etc... } }
personally though think passing in activity
here overkill, when want act on buttons. so, either pass buttons constructor argument instead of activity:
classtwo(button[] btns) { this.btns = btns; }
or, pass in interface
. or consider supplying button
s argument in method, rather storing them in instance variable (i'd go this):
class classtwo { classtwo(){} //default, empty constructor void disableall(button[] btns) { (button btn : btns) { btn.setenabled(false); } } }
Comments
Post a Comment