android - Number picker with Dynamic List -



android - Number picker with Dynamic List -

hey guys i've been implementing numpicker dynamic lists, i.e, changes on event , button click or else. problem whenever switch list larger length smaller length works , smaller length bigger length ain't changes, while set 1st item selected item. crashes. pls. help.i'm new android.

code:

public class mainactivity extends activity { numberpicker numpicker = null; string values1[] = null; string values2[] = null; string values3[] = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); values1 =new string []{ "yello", "red", "white", "green", "black" }; values2 =new string []{ "mango", "orange" }; values3 =new string []{ "class", "pointer", "function", "interafce", "constructor" }; numpicker = (numberpicker)findviewbyid(r.id.numberpicker1); numpicker.setdisplayedvalues(values1); if(values1.length>1) { numpicker.setmaxvalue(values1.length-1); } else { numpicker.setmaxvalue(values1.length); } numpicker.setminvalue(0); numpicker.setvalue(0); numpicker.setbackgroundcolor(1); numpicker.setwrapselectorwheel(false); numpicker.setdescendantfocusability(numberpicker.focus_block_descendants); //on value alter listner changing values /* //for tint values numpicker.setonvaluechangedlistener(new numberpicker.onvaluechangelistener() { @override public void onvaluechange(numberpicker numpic, int arg1, int arg2) { numpickernewval = values[numpic.getvalue()].trim(); tinttemp = getselectedtintvalue(numpickernewval); } });*/ } public void onclick(view v) { switch(v.getid()) { case r.id.button1: if(values1.length>1) { numpicker.setmaxvalue(values1.length-1); } else { numpicker.setmaxvalue(values1.length); } numpicker.setminvalue(0); numpicker.setvalue(0); numpicker.setdisplayedvalues(values1); break; case r.id.button2: if(values2.length>1) { numpicker.setmaxvalue(values2.length-1); } else { numpicker.setmaxvalue(values2.length); } numpicker.setminvalue(0); numpicker.setvalue(0); numpicker.setdisplayedvalues(values2); break; case r.id.button3: if(values3.length>1) { numpicker.setmaxvalue(values3.length-1); } else { numpicker.setmaxvalue(values3.length); } numpicker.setminvalue(0); numpicker.setvalue(0); numpicker.setdisplayedvalues(values3); break; } } }

logcat:

06-20 13:02:16.712: e/androidruntime(16793): fatal exception: main 06-20 13:02:16.712: e/androidruntime(16793): java.lang.illegalstateexception: not execute method of activity 06-20 13:02:16.712: e/androidruntime(16793): @ android.view.view$1.onclick(view.java:3606) 06-20 13:02:16.712: e/androidruntime(16793): @ android.view.view.performclick(view.java:4211) 06-20 13:02:16.712: e/androidruntime(16793): @ android.view.view$performclick.run(view.java:17446) 06-20 13:02:16.712: e/androidruntime(16793): @ android.os.handler.handlecallback(handler.java:725) 06-20 13:02:16.712: e/androidruntime(16793): @ android.os.handler.dispatchmessage(handler.java:92) 06-20 13:02:16.712: e/androidruntime(16793): @ android.os.looper.loop(looper.java:153) 06-20 13:02:16.712: e/androidruntime(16793): @ android.app.activitythread.main(activitythread.java:5297) 06-20 13:02:16.712: e/androidruntime(16793): @ java.lang.reflect.method.invokenative(native method) 06-20 13:02:16.712: e/androidruntime(16793): @ java.lang.reflect.method.invoke(method.java:511) 06-20 13:02:16.712: e/androidruntime(16793): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:833) 06-20 13:02:16.712: e/androidruntime(16793): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:600) 06-20 13:02:16.712: e/androidruntime(16793): @ dalvik.system.nativestart.main(native method) 06-20 13:02:16.712: e/androidruntime(16793): caused by: java.lang.reflect.invocationtargetexception 06-20 13:02:16.712: e/androidruntime(16793): @ java.lang.reflect.method.invokenative(native method) 06-20 13:02:16.712: e/androidruntime(16793): @ java.lang.reflect.method.invoke(method.java:511) 06-20 13:02:16.712: e/androidruntime(16793): @ android.view.view$1.onclick(view.java:3601) 06-20 13:02:16.712: e/androidruntime(16793): ... 11 more 06-20 13:02:16.712: e/androidruntime(16793): caused by: java.lang.arrayindexoutofboundsexception: length=2; index=4 06-20 13:02:16.712: e/androidruntime(16793): @ android.widget.numberpicker.ensurecachedscrollselectorvalue(numberpicker.java:1722) 06-20 13:02:16.712: e/androidruntime(16793): @ android.widget.numberpicker.initializeselectorwheelindices(numberpicker.java:1537) 06-20 13:02:16.712: e/androidruntime(16793): @ android.widget.numberpicker.setmaxvalue(numberpicker.java:1345) 06-20 13:02:16.712: e/androidruntime(16793): @ com.example.numpicker.mainactivity.onclick(mainactivity.java:85) 06-20 13:02:16.712: e/androidruntime(16793): ... 14 more

call setdisplayedvalues(null) before setmaxvalue in onclick.

android

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -