막무가내 삽질 블로그
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to 본문
Android
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to
joong~ 2020. 5. 14. 11:49728x90
안드로이드에서 공통된 전역변수를 사용시 Application을 사용할 수 있다.
class MyApplication : Application() {
private lateinit var networkManager: NetWorkManager
private lateinit var api: NetworkService
private lateinit var movieRemoeteDataSource: MovieRemoteDataSource
private lateinit var movieLocalDataSource: MovieLocalDataSource
private lateinit var movieDao: MovieDao
lateinit var movieRepository: MovieRepository
override fun onCreate() {
super.onCreate()
inject()
}
private fun inject() {
networkManager = NetWorkManager(applicationContext)
api = NetworkService
movieDao = MovieDatabase.Factory.create(applicationContext).movieDao()
movieLocalDataSource = MovieLocalDataSourceImpl(movieDao)
movieRemoeteDataSource = MovieRemoteDataSourceImpl(api)
movieRepository = MovieRepositoryImpl(movieRemoeteDataSource, movieLocalDataSource, networkManager)
}
}
private fun initView() {
myApplication = application as MyApplication
}
간혹 까먹고 매니페스트에 등록 안해주면 Casting Exception이 발생한다.
AndroidManifest.xml
<application
android:name=".위치/파일 >
/>
까먹지 말자.
'Android' 카테고리의 다른 글
room, shared preference 시각화로 보는 법 (0) | 2020.05.26 |
---|---|
Invoke-customs are only supported starting with Android O (--min-api 26) (0) | 2020.05.26 |
코루틴 제어 (0) | 2020.05.10 |
코틀린 코루틴 기초 (0) | 2020.05.10 |
안드로이드 디버깅 with Wifi (android adb wifi) 하는 법 (Mac) (0) | 2020.04.17 |
Comments