android - How to declare intent,class,buttons in one loop? -



android - How to declare intent,class,buttons in one loop? -

why doesn't work?

public class mainactivity extends activity { button[] b = null; intent[] intent=null; class[] klasa; typeface font; textview glavninaslov; imageview plavi,ljubicasti; animationdrawable plavian,ljubicastian; string[] naslovi; int i=0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string[] naslovi = {"","first","second","third","fourth","fifth"}; font = typeface.createfromasset(getassets(),"fatmarker.ttf"); b = new button[] { ( button ) findviewbyid(r.id.button1), ( button ) findviewbyid(r.id.button2), ( button ) findviewbyid(r.id.button3), ( button ) findviewbyid(r.id.button4), ( button ) findviewbyid(r.id.button5), }; for(i=1;i<6;i++) { klasa[i]=class.forname(naslovi[i]);//problem in:"class not found exception" intent[i] = new intent(mainactivity.this,klasa[i]); b[i].setonclicklistener(new button.onclicklistener() { public void onclick(view v) { startactivity(intent[i]); gasenje(); } }); }

can help me...? had similar problems? please help me... rest of code cannot impact on program... please help me... appreciate it...

i have updated code...it starts mainactivity when click on button crunch app... button[] b = null; intent[] intent=null; typeface font; textview glavninaslov; imageview plavi,ljubicasti; animationdrawable plavian,ljubicastian;

int i=0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); class[] naslovi = { first.class, second.class, third.class, fourth.class, fifth.class}; font = typeface.createfromasset(getassets(),"fatmarker.ttf"); b = new button[] { ( button ) findviewbyid(r.id.button1), ( button ) findviewbyid(r.id.button2), ( button ) findviewbyid(r.id.button3), ( button ) findviewbyid(r.id.button4), ( button ) findviewbyid(r.id.button5), }; intent = new intent[b.length]; for(i=0;i<5;i++) { intent[i] = new intent(mainactivity.this,naslovi[i]); b[i].settag(i); b[i].setonclicklistener(new button.onclicklistener() { public void onclick(view v) { startactivity(intent[(integer)v.gettag()]); gasenje(); } }); }

this code works...thanks guys

update:

rather using reflection create these classes, kcoppock suggested should this:

class[] naslovi = { first.class, second.class, third.class, fourth.class, fifth.class};

and in loop:

intent[i] = new intent(mainactivity.this, naslovi[i]); original answer:

assuming have classes called first, second, etc, need include bundle name:

string[] naslovi = {"", "com.package.first", "com.package.second", "com.package.third", "com.package.fourth", "com.package.fifth"};

replace com.package bundle each class.

also after prepare that, foresee you'll indexoutofboundsexception - remove blank "" element naslovi, , alter for loop for(i=0;i<5;i++)

android

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -