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);