简体   繁体   English

Dagger 2构建在单个Inject注释上失败

[英]Dagger 2 build fails on single Inject annotation

I've been trying to figure this out now, for multiple hours, and any help would be appreciated. 我已经尝试了好几个小时,现在已经弄清楚了,任何帮助将不胜感激。

So far my project has been building perfectly fine with Dagger 2. I added another instance field to one of my fragments with the @Inject annotation, added the void inject(MyFragment mf) method into the Component I was using. 到目前为止,我的项目已经使用Dagger 2完美地构建了。我使用@Inject批注将另一个实例字段添加到我的一个片段中,将void inject(MyFragment mf)方法添加到了我正在使用的Component中。 However, after adding this @Inject annotation I get an error saying that it cannot find symbol DaggerAppComponent or DaggerLoginComponent in my Application class (I've commented out the @Inject, and the build worked immediately afterwards). 但是,在添加此@Inject批注之后,我收到一条错误消息,指出它cannot find symbol DaggerAppComponent or DaggerLoginComponent在我的Application类中cannot find symbol DaggerAppComponent or DaggerLoginComponent (我已经注释掉@Inject,并且此后此版本立即cannot find symbol DaggerAppComponent or DaggerLoginComponent )。

I made sure to check that all of my modules' methods have the @Provides annotation just in case, and they're all fine. 我确保检查所有模块的方法都带有@Provides注释,以防万一,并且它们都很好。

Again, thanks in advance! 再次感谢您!

Here is my Application class that provides my components using static methods: 这是我的Application类,它使用静态方法提供我的组件:

public class CleanupApplication extends Application {

private static AppComponent appComponent;
private static LoginComponent loginComponent;

public static AppComponent getAppComponent() {
    return appComponent;
}

public static LoginComponent getLoginComponent() {
    return loginComponent;
}

public static void setComponents(MainActivity ma) {
    appComponent= DaggerAppComponent.builder().appModule(new AppModule(ma)).build();
    //Update LoginComponent accordingly
    loginComponent= DaggerLoginComponent.builder().appComponent(appComponent).loginModule(
            new LoginModule()).build();
}

@Override
public void onCreate() {
    super.onCreate();
    LeakCanary.install(this);
}


}

and the LoginComponent: 和LoginComponent:

@Component(dependencies = {AppComponent.class}, modules =   {LoginModule.class})
 public interface LoginComponent {
 void inject(LoginListFragment llf);
 void inject(LoginDialogFragment ldf);
 Logins logins();
 MyLogins myLogins();
 }

and the fragment that I'm injecting into that causes the error: 我注入的片段会导致错误:

 public class LoginDialogFragment extends DialogFragment implements ExtendedFragment {
 final int MIN_PER_HOUR = 60, SEC_PER_MIN = 60, SEC_PER_MILLISEC =  1000;


ScrollView mDialogLayout;

@Inject  //This one works just fine, using an upstream component
Context ctx;

@Bind(R.id.login_dialog_date_picker)
DatePicker mDatePicker;
@Bind(R.id.login_dialog_time_picker)
TimePicker mTimePicker;
@Bind(R.id.login_dialog_first_name_edit_text)
EditText mFirstName;
@Bind(R.id.login_dialog_last_name_edit_text)
EditText mLastName;
@Bind(R.id.login_dialog_confirmation_code_edit_text)
EditText mConfirmationCode;



@Inject  //THIS IS WHERE IT FAILS
MyLogins loginList;

private int loginPosition;
private MyLogin login;
private MyLoginEvent loginEvent;
private OnSuccessfulCompletion onCompletion;


boolean mIsEdit;


private String mLoginType = null;

public LoginDialogFragment() {
    this(false, -1);
}
public LoginDialogFragment(boolean changeLogin, int pos) {
    this.mIsEdit = changeLogin;
    this.loginPosition=pos;
    CleanupApplication.getLoginComponent().inject(this); //WHERE I INJECT MY LOGINCOMPONENT TO MEMBER FIELDS

}

@Override
public Dialog onCreateDialog(Bundle bundle) {
    mDialogLayout = (ScrollView) getActivity().getLayoutInflater().inflate(R.layout.fragment_login_dialog, null);
    ButterKnife.bind(this, mDialogLayout);


    mDatePicker.setCalendarViewShown(false);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    dialogBuilder.setTitle(mLoginType);
    //dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_login_dialog, null));
    dialogBuilder.setView(mDialogLayout);
    //.setIcon(id)


    if (mIsEdit && loginPosition >= 0) {

        login= (SouthwestLogin) loginList.get(loginPosition);
        Date flightDate=login.getMyLoginEvent().getFlightDate();
        Calendar cal= Calendar.getInstance();
        cal.setTime(flightDate);
        mDatePicker.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)
                , cal.get(Calendar.DAY_OF_MONTH) );
    if(Build.VERSION.SDK_INT>=23) {
        mTimePicker.setHour(cal.get(Calendar.HOUR));
        mTimePicker.setMinute(cal.get(Calendar.MINUTE));
    } else {
        mTimePicker.setCurrentHour(cal.get(Calendar.HOUR));
        mTimePicker.setCurrentMinute(cal.get(Calendar.MINUTE));
    }

        dialogBuilder.setPositiveButton(R.string.dialog_save, dialogClickListener)
                .setNegativeButton(R.string.dialog_cancel, dialogClickListener);

    } else {

        dialogBuilder.setPositiveButton(R.string.dialog_submit, dialogClickListener).
                setNegativeButton(R.string.dialog_cancel, dialogClickListener); //TODO);
    }

    return dialogBuilder.create();
}



 }

EDIT: In by gradle build file, I am using the apt plugin and providing the glassfish annotations. 编辑:在gradle构建文件中,我正在使用apt插件并提供glassfish注释。 Note, in android studio, while coding it asks to auto import the respective generated components, but when pressing the build button it results in this compile error. 请注意,在android studio中,在编码时会要求自动导入各自生成的组件,但是在按下build按钮时会导致此编译错误。

Normally, all you need to do is add the following dependency to your gradle file: 通常,您需要做的就是将以下依赖项添加到gradle文件中:

provided 'org.glassfish:javax.annotation:10.0-b28' //needed to resolve compilation errors

But we'd need the exact compilation errors and possibly your dependencies if the problem persists, and your LoginModule . 但是,如果问题仍然存在,我们将需要确切的编译错误,可能还需要您的依赖关系,以及LoginModule

Your LoginModule probably does not provide MyLogins . 您的LoginModule可能不提供MyLogins

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM