Rahul
Rahul

Reputation: 387

Dagger 2 presenter injection returning null

I am attempting to add Dagger 2 to my Android Project. My application have following screen

  1. Login extends Base activity
  2. Navigation Activity extends Base activity
  3. MW Activity extents Navigation Activity

Presenter Injection is working fine in Login and navigation activity where as in MW activity it return null

Butter Knife is also not working in MW Activity where as working fine in other activities

Following are my classes Application class

public class abcApplication extends Application {
    ApplicationComponent mApplicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();


        mApplicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
        mApplicationComponent.inject(this);
    }

    public static abcApplication get(Context context) {
        return (abcApplication) context.getApplicationContext();
    }

    public ApplicationComponent getComponent() {
        return mApplicationComponent;
    }

    // Needed to replace the component with a test specific one
    public void setComponent(ApplicationComponent applicationComponent) {
        mApplicationComponent = applicationComponent;
    }


}

Base activity

public class BaseActivity extends AppCompatActivity {
    private ActivityComponent mActivityComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public ActivityComponent activityComponent() {
        if (mActivityComponent == null) {
            mActivityComponent = DaggerActivityComponent.builder()
                    .activityModule(new ActivityModule(this))
                    .applicationComponent(abcApplication.get(this).getComponent())
                    .build();
        }
        return mActivityComponent;
    }

}

Navigation Activity

public class NavigationActivity extends BaseActivity implements NavigationView {

    @Inject
    DataClient mDataClient;

    @Bind(R.id.drawer_layout)
    protected DrawerLayout mDrawerLayout;
    @Bind(R.id.navList)
    ExpandableListView mExpandableListView;

    private ActionBarDrawerToggle mDrawerToggle;
    private String mActivityTitle;
    private ExpandableListAdapter mExpandableListAdapter;
    private List<String> mExpandableListTitle;
    private Map<String, List<String>> mExpandableListData;

    private Map<String, String> activityMap;

    private int lastExpandedPosition = -1;

    @Inject
    NavigationPresenter navigationPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        activityComponent().inject(this);
        ButterKnife.bind(this);
        navigationPresenter.attachView(this);


        }
    @Override
    protected void onDestroy() {
    super.onDestroy();
    navigationPresenter.detachView();
    }
        }

MW Activity

public class MWActivity extends NavigationActivity implements MWView{
    private MWPagerAdapter mMWPagerAdapter;


    @Inject
    MWPresenter MWPresenter;

    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ButterKnife.bind(this);

        MWPresenter.attachView(this);
        MWPresenter.getMarketData();
        }

    }

Logcat : FATAL EXCEPTION: main Process: com.abc.xyz, PID: 21542

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.trading.xyz.ui.main.mw.view.MWActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)

@PreActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)

    public interface ActivityComponent {

        void inject(LoginActivity loginActivity);
        void inject(NavigationActivity navigationActivity);
        void inject(MWActivity mWActivity);
        void inject(MWTabFragment mWTabFragment);
        void inject(MWDetailsActivity mWDetailsActivity);


    }

Upvotes: 1

Views: 2538

Answers (2)

David Medenjak
David Medenjak

Reputation: 34542

You have 2 issues regarding super- / sub types at hand.

  1. Butterknife does not support injection to super types
  2. Dagger does not support injection to sub types

As already pointed out, to solve 2. you would need to call inject in your MWActivity, and to use Butterknife you need to use a ViewHolder pattern within your super class to bind / inject the fields, since it will only inject MWActivity and not NavigationActivity.

Upvotes: 3

Rahul
Rahul

Reputation: 387

Activity Component was not injected activityComponent().inject(this); in MW Activity

public class MWActivity extends NavigationActivity implements MWView{
    private MWPagerAdapter mMWPagerAdapter;


    @Inject
    MWPresenter MWPresenter;

    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        activityComponent().inject(this);
        DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ButterKnife.bind(this);

        MWPresenter.attachView(this);
        MWPresenter.getMarketData();
        }

    }

ActivityComponent (Base Activity)

 public class BaseActivity extends AppCompatActivity {
    private ActivityComponent mActivityComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public ActivityComponent activityComponent() {
        if (mActivityComponent == null) {
            mActivityComponent = DaggerActivityComponent.builder()
                    .activityModule(new ActivityModule(this))
                    .applicationComponent(OmsApplication.get(this).getComponent())
                    .build();
        }
        return mActivityComponent;
    }

}

Upvotes: 1

Related Questions