android - Dagger 2 presenter injection returning null -
i attempting add dagger 2 android project. application have following screen
- login extends base activity
- navigation activity extends base activity
- mw activity extents navigation activity
presenter injection working fine in login , navigation activity in mw activity return null
butter knife not working in mw activity working fine in other activities
following 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 replace component test specific 1 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 start activity componentinfo{com.abc.xyz/com.abc.trading.xyz.ui.main.mw.view.mwactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2318) @ 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); }
you have 2 issues regarding super- / sub types @ hand.
- butterknife not support injection super types
- dagger not support injection sub types
as pointed out, solve 2. need call inject
in mwactivity
, , use butterknife need use viewholder pattern within super class bind / inject fields, since inject mwactivity
, not navigationactivity
.
Comments
Post a Comment