Search code examples
androidmosby

Mosby MVP - FragmentStatePagerAdapter - NPE


For my new app i want to use the Mosby Framework. At the moment i have an activity with two tabs and it works fine for my.

Now i want to implement a new activity with 12 tabs and when i want to switch to a new tab i get a 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)

My working activity use the FragmenPagerAdapter. The new Activity should use FragmenStatePagerAdapter. At this point i get a NPE.

Here is my code

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

My Log "wtf" in the createPresenter is never called... Here a my Dagger-2 component

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

Can anybody help me? I think that the framework works fine and i make a silly mistake... ;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)

Solution

  • Just override the onCreateView method and provide a layout. Otherwise the presenter ist null and mosby will throw a NPE for this.