NullPointerException:对于空对象引用的android.support.v4.app.FragmentHostCallback.getHandler()

时间:2017-01-03 10:36:30

标签: android android-viewpager android-tabs fragmentmanager

我正面临以下问题。

情景:

我第一次通过应用程序注册时,我能够看到标签和内容但是当我注销并尝试登录查看相同的代码时,我得到以下异常:

  

NullPointerException:尝试调用虚方法   ' android.os.Handler   android.support.v4.app.FragmentHostCallback.getHandler()'在null   对象参考

但是当打开导航菜单并在之后选择时,我会看到数据和标签显示viewpager显示标签。

内容: 我有2个标签

以下是我的代码:

 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(适配器);   在此行获取nullpointer execption

退出代码:

 Intent intent = new Intent(this, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();

堆栈跟踪:

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)

2 个答案:

答案 0 :(得分:0)

viewPager.setOffscreenPageLimit(3);

是问题所在。因为你有2个标签。这意味着2页,但你强迫3作为PageLimit。还有什么需要添加片段到适配器?

答案 1 :(得分:0)

我有同样的问题,它是由片段的不同实例重用ViewPager实例引起的。第二次调用ViewPager.setAdapter()后出现异常。如果这是您遇到的问题,您可以尝试我使用的解决方案。要解决此问题,我必须在片段的onDestroyView()方法中删除适配器。

项目演示问题和解决方案can be found here