ViewPager FragmentPagerAdapter Nullpointer

ViewPager FragmentPagerAdapter Nullpointer(ViewPager FragmentPagerAdapter Nullpointer)
本文介绍了ViewPager FragmentPagerAdapter Nullpointer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 支持包中使用 ViewPager 时遇到了这个错误.从水平视图滑动 ViewPager 教程

I got this error by using the ViewPager in the Android Support package. from the Horizontal View Swiping with ViewPager Tutorial

06-19 13:07:25.950: E/AndroidRuntime(16382): FATAL EXCEPTION: main
06-19 13:07:25.950: E/AndroidRuntime(16382): java.lang.NullPointerException
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:347)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:342)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.View.measure(View.java:12728)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.View.measure(View.java:12728)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.View.measure(View.java:12728)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.View.measure(View.java:12728)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.View.measure(View.java:12728)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.os.Looper.loop(Looper.java:137)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 13:07:25.950: E/AndroidRuntime(16382):    at dalvik.system.NativeStart.main(Native Method)

推荐答案

以防万一有人没有看passsy的回答的评论,这里是有用的答案的总结:

Just in case anyone doesn't read the comments of passsy's answer, here is a summary of the useful answers:

1) 非常仔细地检查您的 getItem(int index) 方法,并寻找任何可能导致您以 null片段.

1) Examine your getItem(int index) method very closely and look for any logic, scenarios or missing 'break' statements which might cause you to end up with a null fragment.

2) 检查 getCount() 返回的计数是否与 getItem(int index) 中返回的片段数匹配.

2) Check that the count returned by getCount() matches the number of fragments returned in getItem(int index).

3) 检查您的导入并确保您没有将 android.app.Fragmentandroid.support.v4.app.Fragment

3) Examine your imports and ensure you aren't mixing android.app.Fragment with android.support.v4.app.Fragment

这篇关于ViewPager FragmentPagerAdapter Nullpointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)