DAGGER:尝试注入 Fragment 时无法解决错误 "this"?
DAGGER: Error cannot resolve "this" when trying to inject into Fragment?
我在这一行收到 "cannot resolve method" 错误:
AndroidInjection.inject(this);
我正在尝试注入片段。这是整个class:
public class ShowWalletFragment extends BaseFragment {
@Inject
ShowWalletViewModelFactory showWalletViewModelFactory;
ShowWalletViewModel showWalletViewModel;
private Wallet wallet;
private static final String TITLE_BALANCE = "Balance";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
//butterknife bind
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showWalletViewModel = ViewModelProviders.of(this, showWalletViewModelFactory).
get(ShowWalletViewModel.class);
}
@Override
public void onAttach(Context context) {
AndroidInjection.inject(this);
super.onAttach(context);
}
}
为什么不能解析 "this"?有什么建议么?如果您需要我的应用程序的更多代码,请告诉我。
编辑 1:
implementation 'com.google.dagger:dagger:2.19'
annotationProcessor 'com.google.dagger:dagger-compiler:2.19'
implementation 'com.google.dagger:dagger-android:2.19'
implementation 'com.google.dagger:dagger-android-support:2.19'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.19'
使用这个
import dagger.android.support.AndroidSupportInjection
AndroidSupportInjection.inject(this)
我在这一行收到 "cannot resolve method" 错误:
AndroidInjection.inject(this);
我正在尝试注入片段。这是整个class:
public class ShowWalletFragment extends BaseFragment {
@Inject
ShowWalletViewModelFactory showWalletViewModelFactory;
ShowWalletViewModel showWalletViewModel;
private Wallet wallet;
private static final String TITLE_BALANCE = "Balance";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
//butterknife bind
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showWalletViewModel = ViewModelProviders.of(this, showWalletViewModelFactory).
get(ShowWalletViewModel.class);
}
@Override
public void onAttach(Context context) {
AndroidInjection.inject(this);
super.onAttach(context);
}
}
为什么不能解析 "this"?有什么建议么?如果您需要我的应用程序的更多代码,请告诉我。
编辑 1:
implementation 'com.google.dagger:dagger:2.19'
annotationProcessor 'com.google.dagger:dagger-compiler:2.19'
implementation 'com.google.dagger:dagger-android:2.19'
implementation 'com.google.dagger:dagger-android-support:2.19'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.19'
使用这个
import dagger.android.support.AndroidSupportInjection
AndroidSupportInjection.inject(this)