Mosby MVP - FragmentStatePagerAdapter - NPE

时间:2015-12-23 13:49:42

标签: android mosby

对于我的新应用程序,我想使用Mosby Framework。 目前我有一个带有两个标签的活动,它适用于我的。

现在我想实现一个包含12个标签的新活动,当我想切换到新标签时,我会得到一个NPE

java.lang.NullPointerException: Presenter returned from getPresenter() is null
at com.hannesdorfmann.mosby.mvp.delegate.MvpInternalDelegate.getPresenter(MvpInternalDelegate.java:76)
at com.hannesdorfmann.mosby.mvp.delegate.MvpInternalDelegate.detachView(MvpInternalDelegate.java:70)

我的工作活动使用FragmenPagerAdapter。 新的Activity应该使用FragmenStatePagerAdapter。在这一点上,我得到了一个N​​PE。

这是我的代码

//MainActivity
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupViewPager();
        tabLayout.setupWithViewPager(viewPager);
    }


    @Override
    public int getLayout() {
        return R.layout.activity_navigation;
    }

    private void setupViewPager() {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] months = dfs.getMonths();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);

        for (int i = -12; i <= 12; i++) {
            calendar.add(Calendar.MONTH, +1);
            adapter.addFragment(months[calendar.get(Calendar.MONTH)]);
        }
        viewPager.setAdapter(adapter);
        //viewPager.setCurrentItem(11);
        //viewPager.setOffscreenPageLimit(2);

    }

    public static class Adapter extends FragmentStatePagerAdapter {
        private List<String> fragmentTitles = new ArrayList<>();

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

        @Override
        public Fragment getItem(int position) {
            return new OverviewFragmentBuilder(1).build();
        }

        @Override
        public int getCount() {
            return fragmentTitles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitles.get(position);
        }

        private void addFragment(String title) {
            fragmentTitles.add(title);
        }
    }

// Fragment
public class OverviewFragment extends MvpFragment<OverviewView, OverviewPresenter>
    implements OverviewView {

    private OverviewComponent component;

    @Arg
    public int month;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentArgs.inject(this);
        component = DaggerOverviewComponent
            .builder()
            .appComponent(((App) getActivity().getApplication()).getAppComponent())
            .build();
    }

    @Override
    public OverviewPresenter createPresenter() {
        Log.d("wtf", "ever called??");
        return component.presenter();
    }

我的日志&#34; wtf&#34;在createPresenter永远不会被调用... 这是我的Dagger-2组件

@ActivityScope
@Component(dependencies = AppComponent.class)
public interface OverviewComponent {
    OverviewPresenter presenter();
}

任何人都可以帮助我吗?我认为框架工作正常,我犯了一个愚蠢的错误......; D

12-24 15:04:03.800 2547-2547/com.test.testapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.test.testapp, PID: 2547
                                                                             java.lang.NullPointerException: Presenter returned from getPresenter() is null
                                                                                 at com.hannesdorfmann.mosby.mvp.delegate.MvpInternalDelegate.getPresenter(MvpInternalDelegate.java:76)
                                                                                 at com.hannesdorfmann.mosby.mvp.delegate.MvpInternalDelegate.detachView(MvpInternalDelegate.java:70)
                                                                                 at com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegateImpl.onDestroyView(FragmentMvpDelegateImpl.java:71)
                                                                                 at com.hannesdorfmann.mosby.mvp.MvpFragment.onDestroyView(MvpFragment.java:106)
                                                                                 at android.support.v4.app.Fragment.performDestroyView(Fragment.java:2167)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1141)
                                                                                 at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1345)
                                                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:708)
                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                                 at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                 at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                                                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                                 at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
                                                                                 at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
                                                                                 at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
                                                                                 at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778)
                                                                                 at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871)
                                                                                 at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841)
                                                                                 at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115)
                                                                                 at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665)
                                                                                 at android.view.View.performClick(View.java:5198)
                                                                                 at android.view.View$PerformClick.run(View.java:21147)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

只需覆盖onCreateView方法并提供布局即可。否则,演示者将为null,而mosby将为此抛出一个NPE。