android app stops working when spinner values are selected -
whats problem in code below? application stops working when click go after selecting "information technology" branch , "1st" semester (as have fed data in information technology (1st sem) branch only.
check out log image - http://postimg.org/image/6h006a15b/
spinner branch, semester; button go; textview branchtext, semtext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); branch = (spinner) findviewbyid(r.id.spinner1); semester = (spinner) findviewbyid(r.id.spinner2); go = (button) findviewbyid(r.id.button1); arrayadapter adapter = arrayadapter.createfromresource(this, r.array.branch, android.r.layout.simple_spinner_item); branch.setadapter(adapter); arrayadapter adapter1 = arrayadapter.createfromresource(this, r.array.semester, android.r.layout.simple_spinner_item); semester.setadapter(adapter1); branch.setonitemselectedlistener(this); semester.setonitemselectedlistener(this); go.setonclicklistener(this); } @override public void onclick(view arg0) { // todo auto-generated method stub string check = branchtext.gettext().tostring(); string check1 = semtext.gettext().tostring(); if(check.equals("information technology")&&check1.equals("1st")) { intent it=new intent(mainactivity.this, it1stsem.class); startactivity(it); } } @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub switch (parent.getid()) { case r.id.spinner1: branchtext = (textview) view; toast.maketext(this, branchtext.gettext(), toast.length_short).show(); break; case r.id.spinner2: semtext = (textview) view; toast.maketext(this, semtext.gettext(), toast.length_short).show(); break; } } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub }
}
//it1stsem.java
public class it1stsem extends listactivity { string[] data = {"mathematics-i", "physics-i", "chemistry", "engg. mechanics", "electrical science"}; listview l; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); //setcontentview(r.layout.it1stsem); l= (listview) findviewbyid(r.id.listview1); //=getlistview(); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, data); l.setadapter(adapter); } @override protected void onlistitemclick(listview l, view v, int position, long id) { // todo auto-generated method stub textview temp = (textview) v; toast.maketext(this, ""+temp.gettext()+ " "+position, toast.length_short).show(); }
}
//values - strings.xml
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">kiit syllabus</string> <string name="hello_world">hello world!</string> <string name="menu_settings">settings</string> <string-array name="branch" > <item >civil</item> <item >mechanical</item> <item >automobile</item> <item >electrical</item> <item >electronics & tele-communication</item> <item >computer science</item> <item >information technology</item> <item >electronics & electrical</item> <item >electronics & instrumentation</item> </string-array> <string-array name="semester"> <item >1st</item> <item >2nd</item> <item >3rd</item> <item >4th</item> <item >5th</item> <item >6th</item> <item >7th</item> <item >8th</item> </string-array>
//setcontentview(r.layout.it1stsem);
why did comment out line? think that's why cannot find listview1 , nullpointerexception. try uncommenting line.
Comments
Post a Comment