Android ViewPager FragmentPagerAdapter 空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11099897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ViewPager FragmentPagerAdapter Nullpointer
提问by passsy
I got this error by using the ViewPager in the Android Support package. from the Horizontal View Swiping with ViewPager Tutorial
我通过使用 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)
回答by Chris Knight
Just in case anyone doesn't read the comments of passsy's answer, here is a summary of the useful answers:
以防万一有人没有阅读 passsy 答案的评论,这里是有用答案的摘要:
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 nullfragment.
1)getItem(int index)非常仔细地检查您的方法并查找可能导致您以null片段结尾的任何逻辑、场景或丢失的“break”语句。
2) Check that the count returned by getCount()matches the number of fragments returned in getItem(int index).
2) 检查 返回的计数是否与 中返回getCount()的片段数匹配getItem(int index)。
3) Examine your imports and ensure you aren't mixing android.app.Fragmentwith android.support.v4.app.Fragment
3)检查您的进口并确保您没有android.app.Fragment与android.support.v4.app.Fragment
回答by passsy
It's a simple solution. I mixed android.app.Fragmentand android.support.v4.app.Fragment
这是一个简单的解决方案。我混合了android.app.Fragment和android.support.v4.app.Fragment
hope this helps someone
希望这有助于某人

