[Android] viewpager solution for getting NULL pointer of current view

Source: Internet
Author: User

As required, I need to obtain the current view of viewpager.

So I used the following statement:

View currentview = mviewpager. getchildat (mviewpager. getcurrentitem ());

Then I took this view to do other things, but after sliding more than three views, I found it was null, mviewpager. when getcurrentitem () exceeds 2, it is null. This indicates that viewpager is reused by three views.

What should we do?

Reprinted please indicate the source: http://blog.csdn.net/ethan_xue/article/details/7890888

With the help of Google Baidu, we can use findviewwithtag.

In the onpageselected () method in the listener of viewpager, set the tag for the view.

Itemview. settag (currentid );

Then, you only need to find the tag.

View currentview = mviewpager. findviewwithtag (mviewpager. getcurrentitem ()

References:

Http://www.coder007.net/questions/1401/androi-viewpaper-view-childview

Http://www.apkbus.com/blog-90729-39689.html

Http://blog.sina.com.cn/s/blog_7033e38201015kwe.html

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.