android - Adding multiple functions to a ViewPager -
i new android development , trying create sliding carousel buttons link off other activities, have far...
main.java
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //remove title bar this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); //sliding carousel controls mypageradapter adapter = new mypageradapter(); viewpager mypager = (viewpager) findviewbyid(r.id.myfivepanelpager); mypager.setadapter(adapter); mypager.setcurrentitem(1); } //sliding carousel controls class mypageradapter extends pageradapter { public int getcount() { return 5; } public object instantiateitem(view collection, int position) { layoutinflater inflater = (layoutinflater) collection.getcontext() .getsystemservice(context.layout_inflater_service); int resid = 0; switch (position) { case 0: resid = r.layout.carousel_1; break; case 1: resid = r.layout.carousel_2; break; case 2: resid = r.layout.carousel_3; button mybutton = (button) findviewbyid(r.id.paymybill); mybutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent intent = new intent(); setresult(result_ok, intent); finish(); } }); break; case 3: resid = r.layout.carousel_4; break; case 4: resid = r.layout.carousel_5; break; } view view = inflater.inflate(resid, null); ((viewpager) collection).addview(view, 0); return view; } @override public void destroyitem(view arg0, int arg1, object arg2) { ((viewpager) arg0).removeview((view) arg2); } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == ((view) arg1); } @override public parcelable savestate() { return null; }
i trying button working on case 2 @ moment (but case's have 2 4 buttons).
when load onto emulator
button mybutton = (button) findviewbyid(r.id.paymybill); mybutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent intent = new intent(); setresult(result_ok, intent); finish(); } }); break;
commented out works perfectly, when include it crashes on load.
i think problem not correctly pointing button @ pager view , app crashing because main_activity doesn't have of buttons or layouts referencing.
i have spent last 2 days digging through similar problems not sure doing wrong, example, this case
v = inflater.inflate(r.layout.dashboard_media, null);
seams same problem using different implimentation of adapter , not sure can replace , can't (i have tried , nothing seams work), new , sure simple missing!
can help?
ideally, replacing following line
button mybutton = (button) findviewbyid(r.id.paymybill);
with
button mybutton = (button) view.findviewbyid(r.id.paymybill);
would have solved issue.
but in case, carousel_3.xml
button paymybill
how in paymybill button add following
android:onclick="paymybill"
and add function in java handle want do
public void paymybill(view view){ intent intent = new intent(); setresult(result_ok, intent); finish(); }
Comments
Post a Comment