Dagger 2 Presenter 注入返回空值
Dagger 2 presenter injection returning null
我正在尝试将 Dagger 2 添加到我的 Android 项目中。
我的应用程序有以下屏幕
- 登录扩展基础 activity
- 导航 Activity 扩展基础 activity
- MW Activity 扩展导航 Activity
Presenter 注入在登录和导航中工作正常 activity 而在 MW activity 中它 return null
黄油刀在 MW 中也不工作 Activity 而在其他活动中工作正常
以下是我的 classes
申请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;
}
}
基础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;
}
}
导航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();
}
}
兆瓦 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 :
致命例外:主要
进程:com.abc.xyz,PID:21542
java.lang.RuntimeException: 无法启动 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);
}
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();
}
}
Activity组件(基础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;
}
}
您手头有 2 个关于超/子类型的问题。
- Butterknife not support injection to super types
- 匕首确实
如前所述,要解决问题 2,您需要在 MWActivity
中调用 inject
,而要使用 Butterknife,您需要在 super [=26= 中使用 ViewHolder 模式] 绑定/注入字段,因为它只会注入 MWActivity
而不是 NavigationActivity
.
我正在尝试将 Dagger 2 添加到我的 Android 项目中。 我的应用程序有以下屏幕
- 登录扩展基础 activity
- 导航 Activity 扩展基础 activity
- MW Activity 扩展导航 Activity
Presenter 注入在登录和导航中工作正常 activity 而在 MW activity 中它 return null
黄油刀在 MW 中也不工作 Activity 而在其他活动中工作正常
以下是我的 classes 申请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;
}
}
基础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;
}
}
导航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();
}
}
兆瓦 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 : 致命例外:主要 进程:com.abc.xyz,PID:21542
java.lang.RuntimeException: 无法启动 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);
}
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();
}
}
Activity组件(基础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;
}
}
您手头有 2 个关于超/子类型的问题。
- Butterknife not support injection to super types
- 匕首确实
如前所述,要解决问题 2,您需要在 MWActivity
中调用 inject
,而要使用 Butterknife,您需要在 super [=26= 中使用 ViewHolder 模式] 绑定/注入字段,因为它只会注入 MWActivity
而不是 NavigationActivity
.