0

I have gone through all the places in my code in where I am using fragmentpagestateAdapter.

I am using fragmentStatePagerAdapter at four different position. When trying to reproduce this issue the stack trace produced is of different from the one I am encountering with my user and last note: this error is not encountered on all running devices or my test devices.

java.lang.NullPointerException! 
    1. at          android.support.v4.app.FragmentStatePagerAdapter.a(FragmentStatePagerAdapter.jav  a:116)
    2   at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
    3   at android.support.v4.view.ViewPager.populate(ViewPager.java:1020)
    4   at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
    5   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
    6   at android.view.View.measure(View.java:16840)
    7   at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
    8   at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
    9   at android.view.View.measure(View.java:16840)
    10  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    11  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    12  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    13  at android.view.View.measure(View.java:16840)
    14  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    15  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    16  at android.view.View.measure(View.java:16840)
    17  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    18  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    19  at android.view.View.measure(View.java:16840)
    20  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    21  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.enter code herejava:1410)
    22  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    23  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    24  at android.view.View.measure(View.java:16840)
    25  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    26  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    27  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
    28  at android.view.View.measure(View.java:16840)
    29  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2074)
    30  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1234)
    31  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1417)
    32  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131)
    33  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6286)
    34  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
    35  at android.view.Choreographer.doCallbacks(Choreographer.java:591)
    36  at android.view.Choreographer.doFrame(Choreographer.java:561)
    37  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
    38  at android.os.Handler.handleCallback(Handler.java:730)
    39  at android.os.Handler.dispatchMessage(Handler.java:92)
    40  at android.os.Looper.loop(Looper.java:177)
    41  at android.app.ActivityThread.main(ActivityThread.java:5493)
    42  at java.lang.reflect.Method.invokeNative(Native Method)
    43  at java.lang.reflect.Method.invoke(Method.java:525)
    44  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
    45  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
    46  at dalvik.system.NativeStart.main(Native Method)

First pager implementation.

public class MyPagerAdapter extends FragmentStatePagerAdapter {
        private int mCutOffPage;
        private Fragment mPrimaryItem;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            if (i >= mCurrentPageSequence.size()) {
                return null;
            }

            return mCurrentPageSequence.get(i).createFragment();
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO: be smarter about this
            if (object == mPrimaryItem) {
                // Re-use the current fragment (its position never changes)
                return POSITION_UNCHANGED;
            }

            return POSITION_NONE;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position,
                                   Object object) {
            super.setPrimaryItem(container, position, object);
            mPrimaryItem = (Fragment) object;
        }

        @Override
        public int getCount() {
            return Math.min(mCutOffPage + 1, mCurrentPageSequence == null ? 1
                    : mCurrentPageSequence.size());
        }

        public void setCutOffPage(int cutOffPage) {
            if (cutOffPage < 0) {
                cutOffPage = Integer.MAX_VALUE;
            }
            mCutOffPage = cutOffPage;
        }

        public int getCutOffPage() {
            return mCutOffPage;
        }
        public Fragment getPrimaryItem() {
            return mPrimaryItem;
        }
    }

Second Pager Adapter "Iam not using this one in my code ."

adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
//                    case 0 : return new FragmentOn1();
//                    case 1 : return new FragmentOn2();
//                    case 2 : return new FragmentOn3();
                    default: return null;
                }
            }

            @Override
            public int getCount() {
                return 3; // Hard coded 3, because we are displaying 3 fragments
            }
        };

3rd Fragment pager Adapter

  private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        String user_id;
        private SparseArrayCompat<BInterfaceScrollTabHolder> mScrollTabHolders;
        private InterfaceScrollTabHolder mListener;

        public ViewPagerAdapter(FragmentManager fm, String user_id) {
            super(fm);
            this.user_id = user_id;
            mScrollTabHolders = new SparseArrayCompat<InterfaceScrollTabHolder>();
        }

        public void setTabHolderScrollingContent(BInterfaceScrollTabHolder listener) {
            mListener = listener;
        }

        public SparseArrayCompat<BInterfaceScrollTabHolder> getScrollTabHolders() {
            return mScrollTabHolders;
        }

        public Fragment getItem(int num) {

            FragmentScrollTabHolder fragment;
            if (num == 0) {
                fragment = FragmentActivities_.newInstance(this.user_id, num);
            } else if (num == 1) {
                fragment = FragmentFollowers_.newInstance(this.user_id, num);
            } else if (num == 2) {
                fragment = FragmentFollowing_.newInstance(this.user_id, num);
            } else {
                fragment = FragmentActivities_.newInstance(this.user_id, num);
            }

            mScrollTabHolders.put(num, fragment);
            if (mListener != null) {
                fragment.setScrollTabHolder(mListener);
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "\nActivities";
            } else if (position == 1) {
                if (follower_count > 0) {
                    return follower_count + "\nFollowers";
                } else {
                    return "\nFollowers";
                }
            } else if (position == 2) {
                if (following_count > 0) {
                    return following_count + "\nFollowing";
                } else {
                    return "\nFollowing";
                }
            }
            return "\nActivities";
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

        }
    }

fourth pager apdapter .

public class BAdapterFragmentViewPager extends FragmentStatePagerAdapter{
SparseArray<Fragment> mPageReference = new SparseArray<Fragment>();



boolean tab_available ;

public BAdapterFragmentViewPager(FragmentManager fm ) {
        super(fm);
    }
public void setIs_contest_tab_available(boolean tab_available ) {
    this.tab_available = tab_available ;
    notifyDataSetChanged();
}
    @Override
    public Fragment getItem(int num) {
        Fragment fragment = null;
        if (num == 0) {
            fragment = FragmentGeneral_.newInstance();
        } else if(num == 1) {
            fragment = BFragmentSolvedCases_.newInstance();
        } else if(num == 2) {
            fragment = FragmentTrending_.newInstance();
        }else if(num == 3 && tab_available ){
            fragment = FragmentContest_.newInstance();
        }
        mPageReference.put(num, fragment);

        return fragment;
    }

    @Override
    public int getCount() {
        int size ;
        if(tab_available ) {
            size =  4;
        }else{
            size =  3;
        }

        return size ;

    }

    @Override
    public CharSequence getPageTitle(int position) {

        if (position == 0) {
            return "All";
        } else if (position == 1) {
            return "Solved";
        } else if (position == 2) {
            return "Trending";
        }else if( position == 3 && is_contest_tab_available){
            return "Contest";
        }
        return "All";
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }




public Fragment getFragment(int key) {
    return mPageReference.get(key);
}

}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • http://stackoverflow.com/q/218384/1440565 – Code-Apprentice Oct 22 '16 at 09:32
  • An NPE is where you believe a variable contains an object, but in fact it is null. This usually comes about from a situation where an instantiation of a class failed but you did not check for it, or you believe you have passed a valid object to a method but have not done so. – halfer Oct 22 '16 at 09:35
  • You need to check your stack trace and find out which line has crashed. If you can edit your question to indicate definitively which line causes the NPE then someone may be able to say how that code can be improved. – halfer Oct 22 '16 at 09:36
  • @halfer thanks for your explanation i am aware of NPE but the issue is that i am not able pin point the location which is causing this npe and when explicitly passing null instead of the object in my implementation of different fragment state pager adapter i am not able to reproduce the same stack trace which my users are facing . – Sonal Vij Chinioti Oct 22 '16 at 09:45
  • @halfer the second pager adapter which is able to produce the same exact stack trace is not being used in my code . – Sonal Vij Chinioti Oct 22 '16 at 09:46
  • Is the stack trace above from your users who can replicate the crash? If not, sourcing that exact stack trace (e.g. using crash analytics systems) would be a good idea. – halfer Oct 22 '16 at 09:47
  • Otherwise have you tried cloud mobile testing services? There should be something like BrowserStack for mobile devices, which can run your code on a number of different mobile devices and report any crashes back to you. I have done done this myself, but someone is sure to offer it as a service. – halfer Oct 22 '16 at 09:49
  • @halfer the above stack trace is from is from a crash analytics system . the worst part is that on an account 2 to 4 users daily face this issue being new to android development i am unable to fix this . – Sonal Vij Chinioti Oct 22 '16 at 09:55
  • Do you have any feedback from your system (or from user reports) as to what they are doing when the crash happens? e.g. what did they just click on? What screen were they on? Does it affect a common Android version? Your priority is to replicate it on an emulator or a device in your possession (or, as I say, via a [cloud testing service](https://duckduckgo.com/?q=android+app+cloud+testing)). – halfer Oct 22 '16 at 09:57
  • @halfer yeah i can check that out . – Sonal Vij Chinioti Oct 22 '16 at 10:01

0 Answers0