注入后的对象为空

时间:2018-12-29 10:46:49

标签: java android dagger-2

我正在尝试将单例对象注入2个活动。
我的问题是,@ Inject之后的Milk and Coffee对象在Activity中为空。 我认为我错过了一些事情。
请帮助我,告诉我代码中缺少什么

AppComponent:

@Singleton
@Component(modules = {CoffeeModule.class, MilkModule.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder appModule(MyMvpApp myMvpApp);

        AppComponent build();

    }
    void inject(MyMvpApp app);
}

CoffeeModule:

@Module
public class CoffeeModule {

    @Provides
    @Singleton
    Coffee getCoffee(){
        return new Coffee();
    }

}

MilkModule:

@Module
public class MilkModule {

    @Provides
    @Singleton
    Milk getMilk(){
        return new Milk();
    }

}

我的班级应用程序:

public class MyMvpApp extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        initDagger();
    }

    private void initDagger() {
        AppComponent appComponent = DaggerAppComponent.builder()
                .appModule(this)
                .build();
        appComponent.inject(this);
    }

}

这些对象为空的我的活动:

public class LoginActivity extends AppCompatActivity {

    @Inject
    Milk milk;

    @Inject
    Coffee coffee;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.i("tag", "" + milk + coffee);
    }
}

2 个答案:

答案 0 :(得分:1)

您必须在AppComponent类中再添加两行。

USE data_warehouse
GO

CREATE VIEW dbo.products_yearly_v2
WITH SCHEMABINDING 
AS 
SELECT  
    p.product_id AS product_id, 
    p.product_description AS product_name,
    d.order_year AS order_year,  
    SUM(s.order_total) AS sal_by_dept
FROM [dbo].orders_fact AS s
JOIN [dbo].time_dimension AS d ON s.time_id = d.order_date
JOIN [dbo].product_dimension AS p ON s.product_id = p.product_id
GROUP BY d.order_year, p.product_id, p.product_description;
GO

-- index on products_yearly_v2 not products_yearly_v1
CREATE UNIQUE CLUSTERED INDEX IDX_V2
   ON dbo.products_yearly_v2 (order_year, product_id);

编辑1:您还需要在应用程序组件中注入活动。

void inject(MyMvpApp app);

//you need to provide milk and coffee here
Milk getMilk();
Coffee getCoffee();

并以与在Application类中相同的方式在活动中调用initDagger方法。

编辑2:如果您不想重复整个过程,请在应用程序类中创建一个方法,如下所示:

void inject(LoginActivity loginActivity);

在Application类的onCreate内,调用这些方法。

 private AppComponent appComponent ;

 public AppComponent getAppComponent(){
  if (appComponent == null) {
        appComponent = DaggerAppComponent.builder()
            .appModule(this)
            .build();
    }
    return appComponent;
}

 public void initDagger(){
  appComponent.inject(this);
}

在活动中,您可以这样做,而不是一次又一次地初始化整个模块。

getAppComponent();
initDagger();

答案 1 :(得分:1)

@Singleton
@Component(modules = {CoffeeModule.class, MilkModule.class})
public interface AppComponent {

  void inject(LoginActivity app);

  @Component.Builder
  interface Builder {
    @BindsInstance
    Builder appModule(MyMvpApp myMvpApp);

    AppComponent build();
  }
}



public class LoginActivity extends AppCompatActivity {
  @Inject
  Milk milk;

  @Inject
  Coffee coffee;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DaggerAppComponent.builder().appModule((MyMvpApp) getApplicationContext()).build().inject(this);
    Log.i("tag", "" + milk + coffee);
  }
}


public class MyMvpApp extends Application {

  @Override
  public void onCreate() {
    super.onCreate();
    initDagger();
  }

  private void initDagger() {
    AppComponent appComponent = DaggerAppComponent.builder()
        .appModule(this)
        .build();
  }
}