Dagger 2主持人注射返回null

时间:2016-01-27 10:23:53

标签: android dependency-injection dagger-2 butterknife

我正在尝试将Dagger 2添加到我的Android项目中。 我的申请有以下屏幕

  1. 登录扩展基本活动
  2. 导航活动扩展基本活动
  3. MW活动范围导航活动
  4. Presenter Injection在登录和导航活动中工作正常,而在MW活动中它返回null

    Butter Knife也没有在MW活动中工作,在其他活动中工作正常

    以下是我的课程 申请类

    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;
        }
    
    
    }
    

    基础活动

    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;
        }
    
    }
    

    导航活动

    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活动

    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: 致命异议:主要                                                              处理:com.abc.xyz,PID:21542

    java.lang.RuntimeException:无法启动活动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);
    
    
        }
    

2 个答案:

答案 0 :(得分:3)

您手头有2个关于超级/子类型的问题。

  1. Butterknife not support injection to super types
  2. Dagger做not support injection to sub types
  3. 正如已经指出的,要解决2.你需要在你的inject中调用MWActivity,并使用Butterknife你需要在超类中使用ViewHolder模式来绑定/注入字段,因为它只会注入MWActivity而不是NavigationActivity

答案 1 :(得分:1)

  

活动组件未注入activityComponent()。inject(this);   在MW活动中

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(基本活动)

 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;
    }

}