Casting spinners in android
Question
I'm currently trying out the google's various android examples at http://developer.android.com/guide/tutorials/views/hello-spinner.html
BUT I can't seem to get this part to work even though the code is exactly the same as the one provided in the website.
Spinner s = (Spinner) findViewById(R.id.spinner);
Eclipse keeps saying "Cannot cast from View to Spinner"
Anyone of you guys kno how to resolve this?
Thanks.
Solution
Are you sure you imported android.widget.Spinner
and not something else named the same thing? Maybe try the following, just to be sure:
android.widget.Spinner s = (android.widget.Spinner) findViewById(R.id.spinner);
If it doesn't give you a problem there then odds are high that you have a namespace problem.
OTHER TIPS
That seems odd, are you sure you have imported android.widget.Spinner? Spinner inherits from View so it should work.
As the others suggest, this may be a namespace problem. I ran into this once when fouling up a generic -- to use your case, I defined a class as taking Foo<Spinner>
instead of Foo<A>
, and so Spinner
was defined as a local name in my namespace. It was only when I renamed the generic to Foo<A>
that my Spinner
references resolved.
(in my case, it was String
rather than Spinner
, but the pattern should hold)
your spinner variable name should not be same as the class name.