2017-11-25 2 views
0

대거 2를 사용하여 간단한 MVP Archtecture 응용 프로그램을 만들려고합니다.이 튜토리얼에서와 같은 결과를 얻으려고하지만 Kotlin을 사용하고 있습니다. 지금까지 제 코드가 있습니다.대거 2 + Kotlin이보기에 발표자를 삽입 할 수 없습니다.

발표자 :

class MainPresenter @Inject constructor(var view: IMainView): IMainPresenter{ 

override fun beginMessuring() { 
    view.toastMessage("Measuring started") 
} 

override fun stopMessuring() { 
    view.toastMessage("Measuring stopped") 
} 

} 

보기 :

class MainActivity : AppCompatActivity(), IMainView { 

@Inject lateinit var presenter : MainPresenter 

val component: IMainComponent by lazy { 
    DaggerIMainComponent 
      .builder() 
      .mainPresenterModule(MainPresenterModule(this)) 
      .build() 
} 

override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main) 
    component.inject(this) 
    presenter.beginMessuring() 
} 

override fun toastMessage(message: String) { 
    Toast.makeText(this, message, Toast.LENGTH_LONG).show() 
} 
} 

대거 모듈 :

@Module 
class MainPresenterModule(private val view: IMainView) { 
    @Provides 
    fun provideView() = view 
} 

단검 구성 요소 :

@Component(modules = arrayOf(MainPresenterModule::class)) 
interface IMainComponent { 
    fun inject(mainView : IMainActivity) 
} 
,

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maciej.spiritlvl/com.example.maciej.spiritlvl.View.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized 

PS 내 Gradle을 단검 설정 :

kapt 'com.google.dagger:dagger-compiler:2.9' 
mplementation 'com.google.dagger:dagger:2.9' 

편집 : MAINVIEW에 IMainView에서 변경 주입 발표자 유형을

문제는 내가이 시작 오류를 구축 얻고 있다는 것입니다.

답변

1

케이스에 IMainPresenter과 같은 인터페이스를 삽입 할 때마다 사용할 구체적인 구현을 dagger에 알려야합니다. 대거 (dagger)는 당신이 갖고 싶어하는 인터페이스의 구현을 알 수있는 방법이 없습니다 (당신은 그 인터페이스를 다양하게 구현할 수 있습니다).

모듈에 @Provides - 주석이 달린 메서드를 추가하여 IMainView에 올바른 작업을 수행했습니다. 발표자에게도 동일한 작업을 수행 할 수 있지만 모듈을 만들 때 발표자를 직접 만들어야하기 때문에 단검의 모든 부분이 쓸모 없게됩니다.

그렇다면 IMainPresenter 인터페이스를 작업에 삽입하는 대신 콘크리트 구현 MainPresenter을 삽입하십시오. 그런 다음 모듈에 @Provides 메서드가 필요하지 않습니다 (발표자 용).

+0

나는 당신이 언급 한 방법을했다. 보기에서 변경된 필드 유형을 MainPresenter로 바꿨지 만 여전히 kotlin.UninitailizedPropertyException이 발생합니다. – Maciek

관련 문제