【问题标题】:Dagger 2 presenter injection returning nullDagger 2 演示者注入返回 null
【发布时间】:2016-01-27 10:23:52
【问题描述】:

我正在尝试将 Dagger 2 添加到我的 Android 项目中。 我的应用程序有以下屏幕

  1. 登录扩展基本活动
  2. 导航活动扩展基本活动
  3. MW 活动范围导航活动

Presenter Injection 在登录和导航活动中运行良好,而在 MW 活动中它返回 null

黄油刀在 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();
        }

    }

日志猫: 致命例外:主要 进程: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);


    }

【问题讨论】:

  • 请提供你的组件,你的inject(activity)方法
  • @DavidMedenjak 我已经更新了我的描述,请检查
  • @DavidMedenjak 即使黄油刀在 MWActivity 活动中也不起作用

标签: android dependency-injection dagger-2 butterknife


【解决方案1】:

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

  1. 黄油刀做not support injection to super types
  2. 匕首可以not support injection to sub types

正如已经指出的,要解决 2. 您需要在 MWActivity 中调用 inject,并且要使用 Butterknife,您需要在超类中使用 ViewHolder 模式来绑定/注入字段,因为它只会注入MWActivity 而不会注入NavigationActivity

【讨论】:

  • 我已经按照您在评论中提到的方法解决了黄油刀的问题。我忘了在我的回答中提到。非常感谢您的宝贵支持
【解决方案2】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多