DAGGER:尝试注入Fragment时错误无法解决“ this”?

时间:2018-12-20 15:29:02

标签: android dependency-injection dagger-2 dagger

我在此行收到“无法解析方法”错误:

AndroidInjection.inject(this);

我正在尝试插入一个片段。这是整个班级:

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”?有什么建议么?如果您需要我的应用中的更多代码,请告诉我。

EDIT1:

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'

1 个答案:

答案 0 :(得分:1)

使用此

import dagger.android.support.AndroidSupportInjection

AndroidSupportInjection.inject(this)