我一直试图解决这个问题,持续数小时,我们将不胜感激。
到目前为止,我的项目已经与Dagger 2完美搭配。我使用@Inject
注释向我的一个片段添加了另一个实例字段,并将void inject(MyFragment mf)
方法添加到我正在使用的组件中。但是,在添加这个@Inject注释后,我得到一个错误,说我的Application类中有cannot find symbol DaggerAppComponent or DaggerLoginComponent
(我已经注释掉@Inject,并且之后的构建立即生效)。
我确保检查我的所有模块'方法有@Provides
注释以防万一,并且它们都很好。
再次提前谢谢!
这是我的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);
}
}
和LoginComponent:
@Component(dependencies = {AppComponent.class}, modules = {LoginModule.class})
public interface LoginComponent {
void inject(LoginListFragment llf);
void inject(LoginDialogFragment ldf);
Logins logins();
MyLogins myLogins();
}
并且我注入的片段会导致错误:
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();
}
}
编辑:在gradle构建文件中,我使用apt插件并提供glassfish注释。注意,在android studio中,编码时会要求自动导入相应的生成组件,但是当按下构建按钮时会导致编译错误。
答案 0 :(得分:2)
通常,您需要做的就是将以下依赖项添加到您的gradle文件中:
provided 'org.glassfish:javax.annotation:10.0-b28' //needed to resolve compilation errors
但是如果问题仍然存在,我们需要确切的编译错误和可能的依赖关系,以及LoginModule
。
您的LoginModule
可能无法提供MyLogins
。