Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid viewpager adapter and NullPointerException
    primarykey
    data
    text
    <p>Sometimes I'm getting NullPointerExceptions in google play reports, where (as I think) I use viewpager or it's adapter. This is my code:</p> <pre><code>private PagerAdapter mPagerAdapter; private ViewPager mPagerView; private static class PagerAdapter extends FragmentStatePagerAdapter { HashMap&lt;Integer, PageFragment&gt; mPageReferenceMap; public PagerAdapter(FragmentManager fm) { super(fm); mPageReferenceMap = new HashMap&lt;Integer, PageFragment&gt;(); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { PageFragment fragment = PageFragment.newInstance(position); mPageReferenceMap.put(position, fragment); return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); mPageReferenceMap.remove(position); } public PageFragment getFragment(int key) { return mPageReferenceMap.get(key); } @Override public int getItemPosition(Object object) { return POSITION_NONE; } } </code></pre> <p>in onCreate():</p> <pre><code> mPagerAdapter = new PagerAdapter(getSupportFragmentManager()); mPagerView = (ViewPager) findViewById(R.id.pager); </code></pre> <p>Then, at this line I'm getting crash reports from some users:</p> <pre><code> mPagerAdapter.getFragment(mPagerView.getCurrentItem()).whatEverFunctionFromFragment(); </code></pre> <p>I call fragments this way in different places (independent of each other) - in onDismiss function of the dialog, in onItemClick of ListPopupWindow, etc - with different functions in fragment.</p> <p><strong>More info:</strong> I've got this error after I have done everything that sad <a href="https://stackoverflow.com/questions/13897294/android-viewpager-adapter-and-nullpointerexception#comment19255183_13897294">Heinrisch</a>. This error appears when user resumes to the activity. But I still don't understand how to fix it and why is it happening? Should I save <code>mPageReferenceMap</code> in onSaveInstanceState()? or in onStop()? What am I missing in lifecycle of Activity and Fragments?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload