android - How to write a switch statement inside a method -
i have 7 activities a.b,c,d,e,f,g. when click of these buttons, ex:if click a, want go activity. want in switch case using method. tried doing this,but app crashed:
what's wrong in code.y crashing?
package com.example.assignment2; import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.widget.button; import android.widget.textview; public class act_a extends actionbaractivity { textview text; button btn; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_activity_); text = (textview) findviewbyid(r.id.display); text.settext("this activity a"); text.settextsize(20); } public void display(view v) { switch (btn.getid()) { case r.id.buta: intent intent = new intent(this, act_a.class); startactivity(intent); break; case r.id.butb: intent intent1 = new intent(this, act_b.class); startactivity(intent1); break; case r.id.butc: intent intent2 = new intent(this, act_c.class); startactivity(intent2); break; case r.id.butd: intent intent3 = new intent(this, act_d.class); startactivity(intent3); break; case r.id.bute: intent intent4 = new intent(this, act_e.class); startactivity(intent4); break; case r.id.butf: intent intent5 = new intent(this, act_f.class); startactivity(intent5); break; case r.id.butg: intent intent6 = new intent(this, act_g.class); startactivity(intent6); break; } } public void nextactivity(view v) { intent intent = new intent(this, act_b.class); startactivity(intent); } }
public void display(view v) { switch (btn.getid()) { -------------- } }
change btn.getid() v.getid()
you trying access id of uninitialized object btn declared button btn.
do this
public class homeactivity extends actionbaractivity { private textview text; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); text = (textview) findviewbyid(r.id.display); text.settext("this home activity"); text.settextsize(20); } public void display(view v) { intent mintent = null; switch (v.getid()) { case r.id.buta: mintent = new intent(this, aboutactivity.class); break; case r.id.butb: mintent = new intent(this, informationactivity.class); break; case r.id.butc: mintent = new intent(this, contactactivity.class); break; case r.id.butd: mintent = new intent(this, postsactivity.class); break; } if(mintent != null){ startactivity(mintent); //if wanted finish activity uncomment following line //finish(); } } @override public void onbackpressed() { super.onbackpressed(); } }
if wanted call activity , finish current activity should call finish method
intent mintent = new intent(homeactivity.this, aboutactivity.class); startactivity(mintent); finish();
if wanted finish current activity on button,
@override public void onbackpressed() { //write functions need before finishing activity here // if written line **super.onbackpressed();** finish activity on press super.onbackpressed(); }
Comments
Post a Comment