2017-06-15 181 views
1

面临的问题我写的代码下面的代码编译没有错误,但是当我尝试运行,我得到异常DaggerAppComponent没有找到与科特林匕首2

AppModule.kt

@Module 
class AppModule private constructor() { 

@Provides 
fun providesDispatcher(): Dispatcher { 
    return Dispatcher(providesBus()) 
} 

@Provides 
fun providesUserActionCreator(): PnrUserActionCreator { 
    return PnrUserActionCreator(providesDispatcher()) 
} 
@Provides 
fun providesBus(): Bus { 
    return sBus 
} 

companion object { 

    private val sBus = Bus() 
    private var sAppModule: AppModule? = null 

    /** 
    * Gets the app module instance 

    * @return AppModule instance 
    */ 
    val instance: AppModule 
     get() { 
      if (sAppModule == null) { 
       sAppModule = AppModule() 
      } 
      return sAppModule !! 
     } 
} 

AppComponent.kt

@Component(
    modules = arrayOf(AppModule::class) 
) 
interface AppComponent { 

fun inject(mainActivity: MainActivity) 
} 

MainActivity .kt

class MainActivity : AppCompatActivity() { 
@Inject lateinit var mPnrUserActionCreator: PnrUserActionCreator 
@Inject lateinit var mEventBus: Bus 
override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main_screen) 
    DaggerAppComponent.builder().appModule(AppModule.instance) 
      .build().inject(this) 
} 
} 

为的build.gradle文件匕首依赖

kapt { 
    generateStubs = true 
    } 
// Dagger 2 
compile 'com.google.dagger:dagger:2.4' 
kapt 'com.google.dagger:dagger-compiler:2.4' 
provided 'org.glassfish:javax.annotation:10.0-b28' 

有人能告诉我在做什么错了,我应该怎么做,使之正确吗?

+3

你会得到什么错误? – azizbekian

回答

0

DaggerAppComponent()是生成的类。你需要为Dagger“清理和构建”这个项目来生成这个类。

+0

我已经清理并重建了该项目。它正在编译,但运行时给出了错误 – Silky

+0

@Silky检查是否有帮助:https://stackoverflow.com/a/43670496/2686502 – jayeshsolanki93

+0

谢谢,我忘了写适用插件:'kotlin-kapt' – Silky