35

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
  • 5,162
  • 4
  • 39
  • 65

3 Answers3

75

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

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) Check that the count returned by getCount() matches the number of fragments returned in getItem(int index).

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

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
Chris Knight
  • 24,333
  • 24
  • 88
  • 134
  • This should be the accepted answer. The error stack is thrown on all three accounts. In my case I had not implemented a Fragment class inflating the view for a particular tab. I was under the assumption that returning a null from the getItem() method would render the tab (titles) without their associated Fragments in my Activity. Returning a Fragment for each tab from getItem() solved the issue for me. – user1841702 Jul 09 '14 at 19:17
  • happened to me and option 1 was the ticket - just sharing - used a switch case inside getItem(int index), and the case labels were 0, 2, 3, also causes same exception. thanks. – TommySM Sep 09 '15 at 11:51
18

It's a simple solution. I mixed android.app.Fragment and android.support.v4.app.Fragment

hope this helps someone

passsy
  • 5,162
  • 4
  • 39
  • 65
  • 1
    Are you sure that was the fix? I have the exact same stacktrace but no instances of android.app.Fragment ... – brk3 Jun 21 '12 at 22:36
  • you have android.app.Activity or android.app.FragmentActivity? use everything from android.support.v4.app – passsy Jun 25 '12 at 07:58
  • 47
    I found the problem, one of the switch cases in my getItem() was falling out to the bottom where there was a default return null. – brk3 Jun 25 '12 at 11:09
  • 6
    Damm, had a null pointer in getItem as well. So hint to everybody with this error: Check your getItem with a fine comb. – Martin Sep 24 '12 at 12:58
  • brk3, maybe you can add an answer to that effect? Helped me out too! – Robert Karl Feb 07 '13 at 18:41
  • 8
    This happened to me because I was returning a different number in getCount() than the number of tabs. – Sap May 27 '13 at 03:36
6

If getItem() in your Adapter returns null that can cause a null pointer exception.

TisSarah
  • 101
  • 1
  • 3