Viewpager使用`FragmentStatePagerAdapter`制作的轮播导致`NullPointerException`

时间:2017-06-07 12:04:17

标签: android android-fragments android-viewpager fragmentstatepageradapter

作为头衔, 我有一个旋转木马,如下图所示:

Carousel_Normal_status

这是正常状态,当我使用FragmentPagerAdapter时, 一切都很好,但因为我有无限的页面显示在其中, 我不得不使用FragmentStatePagerAdapter来避免快速滑动的车, 但在我做出改变之后:

After using FragmentStatePagerAdapter

许多已定义的行为已经消失,并且已检测到NullPointerException

06-07 18:59:39.003 9266-9266/? I/art: Not late-enabling -Xcheck:jni (already on)
06-07 18:59:39.003 9266-9266/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-07 18:59:39.149 9266-9266/? W/System: ClassLoader referenced unknown path: /data/app/com.kiahstarck.exercise-2/lib/x86
06-07 18:59:39.162 9266-9266/? I/InstantRun: starting instant run server: is main process
06-07 18:59:39.362 9266-9266/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-07 18:59:39.754 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{c30bbd8 #0 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:39.785 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{3891216 #1 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:39.813 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{9e1bd84 #2 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:39.828 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{2fc39a2 #3 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:39.852 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{47f2df0 #4 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:39.877 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{f884dee #5 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:39.932 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{e41391c #6 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.083 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{4af5afa #7 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.095 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{2b6cb08 #8 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.100 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{9e112a1 #9 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.102 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{2082cc6 #10 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.105 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{2439487 #11 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.107 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{5718fb4 #12 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.111 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{45b37dd #13 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.117 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{7ca4f52 #14 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.118 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{baf223 #15 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.120 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{f71f320 #16 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.122 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{65874d9 #17 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.124 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{1a0e9e #18 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.125 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{7c08d7f #19 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.126 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{753214c #20 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.127 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{f528595 #21 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.129 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{11476aa #22 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.131 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{35a829b #23 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.132 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{370638 #24 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.133 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{2a2e611 #25 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.135 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{39b5376 #26 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.136 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{fd4ad77 #27 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.138 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{b724de4 #28 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.140 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{5bed24d #29 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.142 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{ae13102 #30 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.144 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{85caa13 #31 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.144 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{8f86450 #32 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.145 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{6334649 #33 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.146 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{8b55b4e #34 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.147 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{35dd46f #35 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.148 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{287757c #36 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.149 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{360fe05 #37 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.151 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{a8fde5a #38 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.152 9266-9266/com.kiahstarck.exercise W/FragmentManager: moveToState: Fragment state for ItemFragment{89d488b #39 id=0x7f0c005f} not updated inline; expected state 3 found 2
06-07 18:59:40.155 9266-9268/com.kiahstarck.exercise I/art: Do partial code cache collection, code=30KB, data=28KB
06-07 18:59:40.156 9266-9268/com.kiahstarck.exercise I/art: After code cache collection, code=30KB, data=28KB
06-07 18:59:40.156 9266-9268/com.kiahstarck.exercise I/art: Increasing code cache capacity to 128KB
06-07 18:59:40.198 9266-9290/com.kiahstarck.exercise I/OpenGLRenderer: Initialized EGL, version 1.4
06-07 18:59:40.198 9266-9290/com.kiahstarck.exercise D/OpenGLRenderer: Swap behavior 1
06-07 18:59:40.198 9266-9290/com.kiahstarck.exercise W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-07 18:59:40.198 9266-9290/com.kiahstarck.exercise D/OpenGLRenderer: Swap behavior 0
06-07 18:59:40.238 9266-9266/com.kiahstarck.exercise D/onpage: offset   0
06-07 18:59:40.239 9266-9266/com.kiahstarck.exercise W/System.err: java.lang.NullPointerException: Attempt to read from field 'com.kiahstarck.exercise.CarouselRelativeLayout com.kiahstarck.exercise.ItemFragment.root' on a null object reference
06-07 18:59:40.239 9266-9266/com.kiahstarck.exercise W/System.err:     at com.kiahstarck.exercise.CarouselPagerAdapter.onPageScrolled(CarouselPagerAdapter.java:81)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1850)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1824)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1762)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:633)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1706)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.240 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.View.layout(View.java:17637)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5575)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:686)
06-07 18:59:40.241 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.Choreographer.doFrame(Choreographer.java:621)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at android.os.Looper.loop(Looper.java:154)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
06-07 18:59:40.242 9266-9266/com.kiahstarck.exercise W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

突然有两个明显存在的课程无法阅读, 因此它可能是API错误,我检查了解决方案,虽然有很多人面临同样的问题,但细节是完全不同的,它的解决方案也不适合我。

完全不知道,如果有人知道解决方案,将非常感激。 非常感谢!!!

1 个答案:

答案 0 :(得分:0)

通过发现FragmentStatePagerAdapter没有定义Ids&片段的标签。

需要使用适配器的实例化方法来请求片段的引用。