我為 Toothpick 和 Authenticator 創(chuàng)建了 Activity、ViewModel、Modules。@Singletonpublic class GetSmsViewModel { @Inject Application app; @Inject Authenticator authenticator;...}public class GetSmsActivity extends AppCompatActivity { private Scope appScope; @Inject GetSmsViewModel mGetSmsViewModel;... @Override protected void onCreate(Bundle savedInstanceState) { appScope = Toothpick.openScope(getApplication()); appScope.installModules(new DIModule(getApplication()), new DataModule()); super.onCreate(savedInstanceState); Toothpick.inject(this, appScope); ... }}public class DIModule extends Module { public MagicDeliveryMainModule(Application application) { bind(GetSmsViewModel.class).toInstance(new GetSmsViewModel()); bind(Application.class).toInstance(application); bind(Authenticator.class).toInstance(new Authenticator()); }}在 Toothpick 的文檔中寫道:“如果 Toothpick 創(chuàng)建一個(gè)實(shí)例,它將始終注入其依賴項(xiàng)?!?, 但在 Toothpick.inject(this, appScope); 之后mGetSmsViewModel.app == null 和 mGetSmsViewModel.authenticator == null 。在 Toothpick.inject(mGetSmsViewModel, appScope); 之后 app 和 authenticationator 字段被注入。應(yīng)該是這樣嗎?
1 回答

至尊寶的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
在您的示例中,牙簽沒有創(chuàng)建實(shí)例,您正在創(chuàng)建實(shí)例并綁定它。
要讓牙簽創(chuàng)建實(shí)例,需要綁定目標(biāo)實(shí)現(xiàn)類并提供注入構(gòu)造函數(shù)。
添加回答
舉報(bào)
0/150
提交
取消