When you use the findviewbyid () method to obtain the view from layout and perform the corresponding conversion, the message "cannot cast from view to autocompletetextview" is displayed! (Conversion + self-error)

Source: Internet
Author: User

Turn: http://blog.csdn.net/zyz511919766/article/details/7453864

Code:

 1 package zyz.example.autocompletetextview; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6  7 public class AutoCompleteTextView extends Activity { 8     /** Called when the activity is first created. */ 9     @Override10     public void onCreate(Bundle savedInstanceState) {11         super.onCreate(savedInstanceState);12         setContentView(R.layout.main);13          14         AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_country);15         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);16         auttextView.setAdapter(adapter);17     }18     

 

The error "cannot cast from view to autocompletetextview" is displayed on line 1, which also leads to an error in line 2.

Check the cause and no spelling error is found.

For online search, it may be that the corresponding package has not been imported, but you have already pressed the combination key "Ctrl + Shift + O" to import the possible package.

It is also said that it cannot be converted in this way. Android Developers are using this method!


Let's take a closer look. The originally defined class name is exactly the same as the called function name. It's a conflict. No wonder !!!

After modifying the class name, troubleshoot the error !!!!!

OK !!!!!!


Note that

(1) After modifying the class name, you may need to change the name of the ". Java" file accordingly.
(2) After the modification is complete, press Ctrl + Shift + O again to ensure that the required package is imported. Because the previous name conflict may cause you to not import the correct package.
(3) Save the Modification result!

Remember!

October 14, 2014 11:34:09

I just encountered a problem similar to the class author. I may not have slept well last night. Today I create a new class. When binding controls:

ListView lv = (ListView)findViewById(R.id.a);

Error: cannot cast from view to listview

I thought my code was wrong. I thought my life was different when I found out why !! After reading the author's article, I found that:

public class ListView extends Activity {   //类名是什么???????????????????@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.listvview);         ListView lv = (ListView)findViewById(R.id.a);   //控件名又是什么????????????????????ArrayList<String> listdata = new ArrayList<String>();for (int i = 0; i < 400; i++) {listdata.add("Item" + i);}

So you can't be confused when writing programs in the future, or it will make you crazy. Okay, don't talk about it. I ran through my tears ~~ O (>_<) O ~~

 

When you use the findviewbyid () method to obtain the view from layout and perform the corresponding conversion, the message "cannot cast from view to autocompletetextview" is displayed! (Conversion + self-error)

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.