Android Findviewbyid get control back to empty problem how to solve _android

Source: Internet
Author: User

In Android, there are times when you need to load controls in an XML layout that is not in the original activity to enrich the interface of the Android program.

I am in the use of Viewflipper problems encountered.

public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main); 
Inflater= (Layoutinflater) Getsystemservice (layout_inflater_service);
View view=inflater.inflate (R.layout.myview, null);
ImageView view= (ImageView) Findviewbyid (R.id.imageview); 

As we all know, we call Findviewbyid to get the control elements in the layout, but I found a java.lang.NullPointerException error while I was running. At first I thought the view was still empty, but it was found that the ImageView was empty. The IDs for MyView and ImageView have been successfully generated in R.java. After some time searching queries, we find that when we rewrite the adapter GetView () function, we get a control element that is passed through the

 
 

Such a form to obtain.

So it occurred to me that if the Findviewbyid method was invoked directly in the activity, its full form would be This.findviewbyid (). And the control I need is not in the layout of the activity itself, so when I get it, I change it to

 
 

That is, you can get the correct control by invoking the Findviewbyid method from the view where the control is located.

This is not a big problem in itself, but it will take some time if you don't pay attention to it, but I'm just taking notes for myself and making it easier for the vast majority of Android friends to develop.

The above is a small set to introduce the Android Findviewbyid get control back to the empty problem solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.