App force close on viewPager.setAdapter(adapter)

user2781627 picture user2781627 · Dec 23, 2014 · Viewed 8.5k times · Source

I have an application page with viewpager containing two fragments. When my activity pops up , a server request is made for loading current data and then adapter is set. But the app crashes with the following error.

Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference

While debugging, viewPager.setAdapter(adapter) showed null pointer exception. Neither my viewPager is null nor my Adapter.

I am not able to figure out why this is happening.

Please help!! Thanks in advance!!

Answer

user2781627 picture user2781627 · Dec 23, 2014

Found the problem.. In my fragmentPagerAdapter, getItem() was returning null for a fragment.