This is a summary in case it was too big to read: I have created two spinners where the first one reads from an array , and its selected item value is taken by the 2nd spinner and fetched in a db to populate the 2nd spinner. It is working fine except that I can take the value of the selected item of the 1st spinner but not the 2nd spinner ALTHOUGH it does populate.
the error occurs here: String selected = (String) s2.getSelectedItem(); although (again) the spinner is being populated correctly and I can getCount() or get the object or the view of the selectedItem the error is a handler exception - Null Pointer exception as shown by logcat please help On Jun 30, 5:06 pm, Georgy <[email protected]> wrote: > Hello, > > I am trying to get the selected item of my spinner that is populated > and I am already seeing the values. when I try to use > (String) s.getSelectedItem(); to get the selected item, I am receiving > a handler exception on Logcat. > > The spinner throwing the error is a 2nd spinner resulting from the 1st > spinner's selected item (that works perfectly). In other words, user > selects from spinner one and upon selection, populates spinner 2 > (until here everything is fine). When trying to get the value of the > selected row in spinner2 , I am receiving the error. > > here's the main parts of my code: > > @Override > public void onCreate(Bundle icicle) { > > super.onCreate(icicle); > setContentView(R.layout.main); > > // The drop down list for the list of universities > final Spinner s = (Spinner) findViewById(R.id.spinner); > ArrayAdapter adapter = ArrayAdapter.createFromResource( > this, R.array.universities, > android.R.layout.simple_spinner_item); > adapter.setDropDownViewResource > (android.R.layout.simple_spinner_dropdown_item); > s.setAdapter(adapter); > > // Initialization of the 2nd spinner > String[] array = {"this", "is", "driving", "me", "crazy"}; > final Spinner s2 = (Spinner) findViewById(R.id.spinner2); > ArrayAdapter<String> cadapter = new ArrayAdapter(this, > android.R.layout.simple_spinner_item, array); > cadapter.setDropDownViewResource > (android.R.layout.simple_spinner_dropdown_item); > s2.setAdapter(cadapter); > // When user selects a university > s.setOnItemSelectedListener(new OnItemSelectedListener() { > > @Override > public void onItemSelected(AdapterView<?> arg0, View > arg1, > int arg2, long arg3) { > //Get the selected university > String selectedItemString = (String) > s.getSelectedItem(); > // System.out.println > ("Uni: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+selectedItemString); > > if(selectedItemString .equals("")) > { > // In case of an error so it won't crash > nospinner(); > } > > else > { > // Find the table of the selected > University dynamically > finder(selectedItemString); > } > > } > > @Override > public void onNothingSelected(AdapterView<?> arg0) { > // TODO Auto-generated method stub > } > > }); > > // When user selects a university > s2.setOnItemSelectedListener(new OnItemSelectedListener() { > > @Override > public void onItemSelected(AdapterView<?> > arg0, View arg1, > int arg2, long arg3) { > > // String selected = > (String)s2.getSelectedItem(); <---- it > crashes here > // System.out.println > ("Building: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+selected); > } > > @Override > public void onNothingSelected(AdapterView<?> > arg0) { > // TODO Auto-generated method stub > } > }); > > } > > protected void finder(String selectedItemString) { > > final Dbadapter db = new Dbadapter(this); // This creates an > instance of the database in this class > final Spinner s2 = (Spinner)findViewById(R.id.spinner2); > // Access Database > db.open(); > > // Fix selected item to match the db > selectedItemString = (selectedItemString).toLowerCase(); > selectedItemString = selectedItemString.replaceAll(" ",""); > > // Call a function to return the rows of that university > final Cursor cur = db.getAllTitles(selectedItemString); > //cur.moveToFirst(); > > // Get Columns > String[] columns = new String[]{"building"}; > SimpleCursorAdapter cadapter = new > SimpleCursorAdapter(this, > android.R.layout.simple_spinner_item, cur, > columns, new int[] {android.R.id.text1}); > > // Spinner Layout and population > cadapter.setDropDownViewResource > (android.R.layout.simple_spinner_dropdown_item); > s2.setAdapter(cadapter); > db.close(); > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

