막무가내 삽질 블로그

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:49
728x90

안드로이드에서 공통된 전역변수를 사용시 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=".위치/파일 >

 />

 

 

 

 

까먹지 말자.

 

 

 

Comments