I am facing following issue.
Scenario:
For the very first time when I signup thru app, I am able to see tabs and there content but when I logout and try to login for the same code for viewpager i get following exception:
NullPointerException: Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
but when open navigation menu and select after that I get data and tabs are shown viewpager shows tabs.
Contents: I have 2 tabs
Below is my code:
public void setupDashboardTabs(List<CreateFragmentsPojo> fragments) {
this.fragments = fragments;
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupDashboardViewPager(fragments);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setUpFontStyleForTabs();
}
public void setupDashboardViewPager(List<CreateFragmentsPojo> fragments) {
try {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for (int i = 0; i < fragments.size(); i++) {
adapter.addFragment(fragments.get(i).getFragment(), fragments.get(i).getTitle());
}
viewPager.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
viewPager.setAdapter(adapter); getting nullpointer execption at this line
Code to logout:
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Stacktrace:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
W/System.err: at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:1949)
W/System.err: at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1965)
W/System.err: at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
W/System.err: at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
W/System.err: at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:513)
W/System.err: at io.skreem.dashboard.DashboardActivity.setupDashboardViewPager(DashboardActivity.java:459)
W/System.err: at io.skreem.dashboard.DashboardActivity.setupDashboardTabs(DashboardActivity.java:443)
W/System.err: at io.skreem.dashboard.DashboardActivity$4.onResponse(DashboardActivity.java:408)
W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
W/System.err: at android.os.Handler.handleCallback(Handler.java:746)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5443)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
viewPager.setOffscreenPageLimit(3);
is the problem. As you have 2 tabs. Which means 2 pages but you are forcing 3 as the PageLimit. Also whats the need of adding fragment to adapter?