ViewPager NULL pointer error, android. support. v4.view. ViewPager. onSaveInsta

Source: Internet
Author: User

The support. v4 package provides us with a very practical sliding control ViewPager. When using ViewPager, there is a need to note:

That is:

Android. support. v4.view. ViewPager. onSaveInstanceState NULL pointer and so on...

 

The error is as follows:

...

Caused by: java. lang. NullPointerException
At android. support. v4.view. ViewPager. onSaveInstanceState (ViewPager. java: 507)
At android. view. View. dispatchSaveInstanceState (View. java: 6068)
At android. view. ViewGroup. dispatchSaveInstanceState (ViewGroup. java: 1180)


...

...

Problem Analysis:

When you jump to another activity or close the current activity, if your current activity is useful for ViewPager, but you have not yet set the adapter for ViewPager, the above exception will occur;


--

Solution:

As long as there is ViewPager, you must set an adapter for ViewPager during page initialization, whether or not you currently use it or not. In addition, it is recommended that one ViewPager be declared only once and set the adapter once, otherwise the interface may not be displayed sometimes;

 

 

If the ViewPager control appears in the layout code, whether used or not, you must obtain its object in onCreate and setAdapter (). Otherwise, the onSavedInstanceState NULL pointer error will be reported during Activity switching.

 


There are also some error messages such as the current Activity cannot be stopped normally.

This is actually a small bug in this package. Many open-source projects on the Internet have fixed this bug. The core code of ViewPager is modified as follows:

 if (f.mSavedViewState != null) {     if (result == null) {         result = new Bundle();     }     result.putSparseParcelableArray(             FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); } if (!f.mUserVisibleHint) {     // Only add this if it's not the default value     result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); } 


 

After modification:

  if (f.mSavedViewState != null) {     if (result == null) {         result = new Bundle();     }     result.putSparseParcelableArray(             FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); } if (!f.mUserVisibleHint) {     if (result == null) {         result = new Bundle();     }     // Only add this if it's not the default value     result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); 

 

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.