0

I am using a android spinner to select among city names. The problem is that the line where I setAdapter() is crashing my application.

`

   selectCity = (Spinner) findViewById(R.id.spinnerSelectCity);
            ArrayAdapter<CharSequence> spinadapter = ArrayAdapter.createFromResource(main.this, R.array.cities, android.R.layout.simple_spinner_item);  
            spinadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        selectCity.setAdapter(spinadapter);`

selectCity.setAdapter(spinadapter);// line is throwing a java.lang.nullpointerexception.

I also tried using array adapter constructor and using that but same problem.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Rajul
  • 5,906
  • 2
  • 22
  • 26
  • 2
    it probably means `selectCity` is `null`. make sure that spinner with id `spinnerSelectCity` is present in layout and that you call `findViewById` after setting content view. – Vladimir Dec 14 '11 at 08:52
  • Yes As Vladimir said select city is null.Either put try catch and debug you will come to know that what is the problem.If you solved accept Prasham answer – Tofeeq Ahmad Dec 14 '11 at 09:46
  • Ok guys I got my mistake. http://stackoverflow.com/questions/7968573/android-viewpager-findviewbyid-not-working-always-returning-null The above solution solved my issue. Sorry for bothering you guys. – Rajul Dec 14 '11 at 10:33

1 Answers1

0

selectCity is null....

Check the xml you use as view, does it have any spinner whose ID is spinnerSelectCity?

if not

  1. Replace the layout which is having spinnerSelectCity in it
  2. You may have used it in another layout that will not be on use..... move spinnerSelectCityfrom that layout to the layout you use...
Prasham
  • 6,646
  • 8
  • 38
  • 55
  • I think I have a broblem as I am using ViewPager to swipe between views contained in xml files.I tried following steps outlined here http://stackoverflow.com/questions/7968573/android-viewpager-findviewbyid-not-working-always-returning-null as I have a pretty similar setup but its still returning null. – Rajul Dec 14 '11 at 10:16
  • Can you guide as to how I should overcome the problem.Thanks for the help so far. – Rajul Dec 14 '11 at 10:20
  • 1
    Ok guys I got my mistake. http://stackoverflow.com/questions/7968573/android-viewpager-findviewbyid-not-working-always-returning-null The above solution solved my issue. Sorry for bothering you guys. – Rajul Dec 14 '11 at 10:33