title: Dagger with FragmentPagerAdapter - android
body: <p>I am using from Dagger in my <code>ViewPager</code> like bellow.</p>
  <p>My <code>ViewPagerAdapter.java</code>:</p>
  <pre><code>    public class ViewPagerAdapter extends FragmentPagerAdapter {
          private final List&lt;Fragment&gt; mFragmentList; // note: this can cause crashes
          private final List&lt;String&gt; mFragmentTitleList;
          private final FragmentManager fragmentManager;
      
          @Inject
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
              this.fragmentManager = manager;
              this.mFragmentList = new ArrayList&lt;&gt;();
              this.mFragmentTitleList = new ArrayList&lt;&gt;();
          }
      
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
      
          @Override
          public int getCount() {
              return mFragmentList.size();
          }
      
          public void addFragment(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
      
          @Override
          public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
              super.destroyItem(container, position, object);
              // remove destroyed Fragment from FragmentManager
              if (!mFragmentList.contains(object)) {
                  fragmentManager.beginTransaction().remove((Fragment) object).commit();
              }
          }
      
          @Override
          public int getItemPosition(@NonNull Object object) {
              return POSITION_NONE;
          }
      
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitleList.get(position);
          }
      }
  </code></pre>
  <p>And in <code>MainActvity</code> I am using like bellow:</p>
  <pre><code>public class MainActivity extends DaggerAppCompatActivity{
      @Inject
      ViewPagerAdapter viewPagerAdapter;
      private void setupViewPager(ViewPager viewPager) {
          Intent intent = getIntent();
              viewPagerAdapter.addFragment(new WorkTableFragment(), getString(R.string.workTable));
              viewPagerAdapter.addFragment(MouFragment.newInstance(langID, lang), getString(R.string.mou));
              viewPager.setAdapter(viewPagerAdapter);
              tabsMainActivity.setupWithViewPager(viewPager);
              viewPager.setCurrentItem(1);
          }
      }
  }
  </code></pre>
  <p>And my <code>classes</code> of <code>dagger</code> is like bellow. my <code>ActivityBuilder</code>:</p>
  <pre><code>@Module
  public abstract class ActivityBuilder {
      @ContributesAndroidInjector(modules = MainActivityModule.class)
      abstract MainActivity bindMainActivity();
  
  }
  </code></pre>
  <p>And <code>AppComponent</code>:</p>
  <pre><code>@Singleton
  @Component(modules = {
          AndroidSupportInjectionModule.class,
          AppModule.class,
          ActivityBuilder.class})
  public interface AppComponent extends AndroidInjector&lt;DaggerApplication&gt; {
  
      void inject(App app);
  
      @Override
      void inject(DaggerApplication instance);
  
      @Component.Builder
      interface Builder {
          @BindsInstance
          Builder application(Application application);
          AppComponent build();
      }
  }
  </code></pre>
  <p>And <code>AppModule</code>:</p>
  <pre><code>@Module
  public abstract class AppModule {
  
      @Binds
      abstract Context provideContext(Application application);
  
  }
  </code></pre>
  <p>And my <code>MainActivityModule</code>:</p>
  <pre><code>@Module
  public abstract class MainActivityModule {
  
      private static MainActivity mainActivity;
  
      public MainActivityModule(MainActivity mainActivity) {
          this.mainActivity = mainActivity;
      }
  
      @Provides
      static MainPresenter provideMainPresenter(MainContract.MainContractView mainView, DataSourcesLocal dataSourcesLocal) {
          return new MainPresenter(mainView, dataSourcesLocal);
      }
  
      @Provides
      static ViewPagerAdapter provideViewPagerAdapter() {
          return new ViewPagerAdapter(mainActivity.getSupportFragmentManager());
      }
  
      @Binds
      abstract MainContract.MainContractView provideMainView(MainActivity mainActivity);
  }
  </code></pre>
  <p>And my Application class:</p>
  <pre><code>public class App extends DaggerApplication {
      @Override
      protected AndroidInjector&lt;? extends DaggerApplication&gt; applicationInjector() {
          AppComponent appComponent = DaggerAppComponent
                  .builder()
                  .application(this)
                  .build();
          appComponent.inject(this);
          return appComponent;
      }
  }
  </code></pre>
  <p>But get bellow error:</p>
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
  </code></pre>