ViewPager FragmentPagerAdapter空指针(ViewPager Fragme

2019-06-25 14:34发布

我通过在Android使用ViewPager得到这个错误支持包 。 从水平视图与刷卡ViewPager教程

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)

Answer 1:

万一有人不读passsy的回答的意见,这里是有用的答案的摘要:

1)检查您getItem(int index)密切方法,查找任何逻辑,情景或丢失的“破发”的语句这可能会导致你与一个最终null片段。

2)检查返回的计数getCount()在返回的片段的数量相匹配getItem(int index)

3)检查您的进口,确保不要混用android.app.Fragmentandroid.support.v4.app.Fragment



Answer 2:

这是一个简单的解决方案。 我混android.app.Fragmentandroid.support.v4.app.Fragment

希望这可以帮助别人



Answer 3:

如果getItem()时在适配器返回空,可能会导致空指针异常。



文章来源: ViewPager FragmentPagerAdapter Nullpointer