2016-01-27 98 views
1

我正在嘗試將Dagger 2添加到我的Android項目中。 我的應用程序有以下屏幕Dagger 2演示者注入返回null

  1. 登錄擴展基地活動
  2. 導航活動延伸基地活動
  3. 兆瓦的活動程度的導航活動

主持人注射在登錄做工精細和導航的活動,其中作爲在MW活動中返回null

Butter Knife在MW活動中也不工作,其中workin克細其它活動

以下是我的班 應用類

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

兆瓦活動

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。 (android.app.ActivityThread.java:2318) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:)在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); 


    } 
+0

請提供您的組件,你HABE你'注入(活動)'方法 –

+0

@DavidMedenjak我已經更新了我的描述請 – Rahul

+0

@DavidMedenjak甚至黃油刀不MWActivity活動工作 – Rahul

回答

1

Activity組件未被注入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; 
    } 

} 
3

您對超/子類型的手2點的問題。

  1. Butterknife確實not support injection to super types
  2. 匕首確實not support injection to sub types

前面已經指出的那樣,解決2.你需要調用injectMWActivity,並使用Butterknife你需要使用一個ViewHolder因爲它只會注入MWActivity而不是NavigationActivity,所以在您的超類中綁定/注入字段。

+0

我已經爲你提到的做同樣的你的評論用黃油刀解決問題。我忘了在我的回答中提到。非常感謝您的寶貴支持 – Rahul