views:

744

answers:

5

Hi,

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.

EDIT: Thanks guys! I made a noob mistake, my class was also called Spinner hence my Spinner declaration was actually declaring my class instead of the spinner widget, silly me, thanks again!

+3  A: 

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.

fiXedd
+1  A: 

That seems odd, are you sure you have imported android.widget.Spinner? Spinner inherits from View so it should work.

Rexxars
+1  A: 

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)

CommonsWare
A: 

I suppose your class name is same as 'Spinner'. In that case there will be some conflicts.

Vishwanath N B
A: 

That's what happened to me -- what Vishwanath said. I changed the class name and everything was fine.