Android Error: Cannot cast from View to Spinner

Source: Internet
Author: User

Today, an error occurred while implementing the spinner control:

Cannot cast from View to Spinner. As shown in figure:

Private Spinner Spinner01;
spinner01= (Spinner) Findviewbyid (R.ID.SPINNER01);

The reason for the search on the Internet is the problem of importing the package, after checking the code found that it is really the wrong package, as shown:

The reason is that the Java file class name in my own project package Nyist.daylight is also spinner, and the correct one to import should be the Android.widget package.

The solution is to choose Spinner-android.widget,

Private Android.widget.Spinner Spinner01;
spinner01= (Android.widget.Spinner) Findviewbyid (R.ID.SPINNER01);

This allows the program to run normally.

Summary: When you create a new project, it's best not to have the same name and name as the class in Android, or to import the correct package when you're in the package, so you can avoid this error.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.