5

我有一个 Android 应用程序,我正在尝试使用匕首。我决定使用全局图 + 活动图。我有三个模块:

安卓模块

@Module(library = true)
public class AndroidModule {

  private final MApplication mApplication;

  public AndroidModule(MyApplication application) {
    mApplication = application;
  }

  @Provides @Singleton @ForApplication Context provideApplicationContext() {
    return mApplication;
  }
}

我的应用模块

@Module(
    injects = {
            MainActivity.class,
            AddFragment.class,
            AddMapFragment.class,
            MyActivity.class,
            ListFragment.class,
            MyMapFragment.class,
            RetainFragment.class,
            SingleActivity.class,
            UserActivity.class,
            UserLoginFragment.class,
            UserProfileActivity.class,
            UserProfileFragment.class,
            UserRegisterFragment.class,
            WelcomeFragment.class
    },
    complete = false,
    library = true,
    includes = ActivityModule.class
)
public class MyAppModule {

  private final Bus mBus = new Bus();

  @Provides @Singleton Bus provideBus() {
    return mBus;
  }

  @Provides @Singleton
  RequestQueue provideRequestQueue(@ForApplication Context context) {
    return Volley.newRequestQueue(context);
  }

  @Provides @Singleton Api provideApi(RequestQueue queue) {
    return new AppApi(queue);
  }

  @Provides Logger provideLogger() {
    return new MyLogger();
  }
}

活动模块

@Module(
    complete = false,
    library = true
)
public class ActivityModule {

  private BaseActivity mActivity;
  public ActivityModule() {}

  public ActivityModule(BaseActivity activity) {
    mActivity = activity;
  }

  @Provides @Singleton @ForActivity Context provideActivityContext() {
    return mActivity;
  }

  @Provides @Singleton LocationClientFactory provideLocationClientFactory() {
    return new LocationClientFactory(mActivity);
  }
}

我不得不MyAppModule对. ActivityModule_ Context_ActivityContext

图表配置是否正确?我将模块添加到图中Application和 a 中BaseActivity,我认为这就足够了,我不需要包含,但我不确定我是否完全理解它的配置过程。

编辑

像这样,匕首没有错误,但如果我尝试注入一个Activity Context,在运行时它是null.

4

1 回答 1

4

如果有人感兴趣,解决方案是删除

injects = {
        MainActivity.class,
        AddFragment.class,
        AddMapFragment.class,
        MyActivity.class,
        ListFragment.class,
        MyMapFragment.class,
        RetainFragment.class,
        SingleActivity.class,
        UserActivity.class,
        UserLoginFragment.class,
        UserProfileActivity.class,
        UserProfileFragment.class,
        UserRegisterFragment.class,
        WelcomeFragment.class
}

来自 MyAppModule 并将其放入 ActivityModule。

于 2013-06-19T12:51:02.427 回答