2016-08-27 5 views
0

"RepositoryImpl은 @Inject 생성자없이 또는 @이 제공-주석 방법을에서 제공 할 수 없다"단검이 오류 : I 인터페이스 다음 한 예를 들어

public interface Repository { 
    Observable<Pojo> getPojos(); 
} 

및 구현 :

public class RepositoryImpl implements Repository { 

    public RepositoryImpl() { 

    } 

    @Override 
    public Observable<Pojo> getPojos() { 
     return null; 
    } 

} 

모듈 :

@Module 
class AppModule { 

    public AppModule() { 

    } 

    @Provides 
    @Singleton 
    Repository provideRepositoryImpl() { 
     return new RepositoryImpl(); 
    } 

} 

그리고 구성 요소 :

@Singleton 
@Component(modules = { AppModule.class }) 
public interface AppComponent { 
    void inject(MainActivity mainActivity); 
} 

프로젝트를 만들 때 질문 제목과 같은 오류가 나타납니다. 내 코드에서 어떤 문제가 있습니까?

+1

이 단검은 Application 클래스에 주입됩니다 일반적으로 어디 분사의 응용 프로그램 클래스에 대한? –

답변

0

내가 설정 한 단검 2는 내 프로젝트 응용 프로그램에 있습니다. 분사 구성 요소를 추가합니다. 그렇게.

public class NyApplication extends Application { 

InjectionComponent component; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    setDagger(); 
} 

private void setDagger() { 
    component = DaggerAppComponent.builder() 
      .appComponent(new AppModule()) 
      .build(); 
    component.inject(this); 
} 

public InjectionComponent getComponent() { 
    return component; 
}} 

그리고 내 int 활동이 무엇이든간에. 나는 그것에 주사한다 onCreate 이것 같이 on.

public class MainActivity extends Activity { 

@Inject 
Object object; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ((MyApplication) getApplication()).getComponent().inject(this); 

}} 

이 정보가 도움이되기를 바랍니다.

Dagger 2 error: “RepositoryImpl cannot be provided without an @Inject constructor or from an @Provides-annotated method”

는 일반적으로 이것이 당신이 @Inject RepositoryImpl하지 @Inject Repository에 시도한 의미

1

는 오류 신중하게 (강조 광산)를 참조하십시오. Dagger가 @Inject-annotated 생성자를 사용하여 RepositoryImpl을 만들지 않고 모듈이 RepositoryImpl 생성자를 직접 호출하기 때문에 이는 특히 중요합니다. (당신이 있다면, 당신은 당신의 @Provides 방법을 RepositoryImpl에게 매개 변수를 만들거나 @Binds 방법으로 전환, 당신은 구현 대 인터페이스를 주입 사이에 선택의 여지가있을 수 있습니다.)