0

对于我的新应用,我想使用 Mosby 框架。目前我有一个带有两个选项卡的活动,它对我来说很好。

现在我想用 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。在这一点上,我得到了一个 NPE。

这是我的代码

//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();
    }

createPresenter 中的我的日志“wtf”从未被调用...这是我的 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)
4

1 回答 1

1

只需覆盖该onCreateView方法并提供一个布局。否则,presenter ist null 并且 mosby 将为此抛出 NPE。

于 2015-12-25T16:06:36.813 回答