Why can `applicationContext` return null? I'm gett...
# android
p
Why can
applicationContext
return null? I'm getting null when calling it. This is my Application class:
Copy code
class MyApplication: Application() {
    lateinit var container: AppContainer
    override fun onCreate() {
        super.onCreate()
        container = AppContainer()
    }
}

//Extension function that returns an instance of Application
fun CreationExtras.MyApplication(): MyApplication =
    (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MyApplication)
And this is how I get it:
Copy code
MyApplication().applicationContext
not kotlin but kotlin colored 2
m
You can get the
applicationContext
directly from any
Context
(like an
Activity
or
Service
):
Copy code
val appContext = context.applicationContext as MyApplication
p
I don't have a context at that point and I wanna make it from a context different from the current activity, so I need to do it from the applicationContext
but it is null
why?
s
Have you declared that
MyApplication
is the app's Application in the AndroidManifest? Something like:
Copy code
<application
    android:name=".MyApplication"
The
.
is because the class is in the project root, if it's in a different package you will have to do something like
.mypackage.MyApplication
p
yes it is, and it's not null, the only null is the applicationContext variable inside Application object
s
Are you calling
applicationContext
in a
Service
? If not, the only thing I can suggest is that you implement the suggestion in https://stackoverflow.com/questions/21994612/get-application-context-returns-null which is your exact question
p
I did it, and the first time I'm accessing it, I got this error: kotlin.UninitializedPropertyAccessException: lateinit property context has not been initialized
s
It seems that you are accessing the
applicationContext
before the App has been created. When are you trying to access the context?
p
on the init method of the viewmodel of the first screen of the first activity
s
How are you initializing the ViewModel in the Activity?
p
Copy code
@Composable
fun LoadingScreen(
    viewModel: LoadingScreenViewModel = viewModel(factory = LoadingScreenViewModel.factory(LocalContext.current)),
    modifier: Modifier = Modifier
)
in the constructor of the first Composable I'm getting it.
s
You shouldn't pass a ViewModel to Composable you should pass the parameters and stuff you need to the Composable
Besides that why are you passing Context to the ViewModel, but you are saying that you are also using MyApp.applicationContext instead of using the passed Context?
p
I'm doing like that because is that how I learned in the google official codelabs
they started the viewmodel variable on the parameters of the composable
after doing that, they use it normally on the composable, like this
val uiState = viewModel.uiState
about the context passed as a parameter, is because this screen needs to do some stuff that requires the context of the current screen, but in this stuff, some other tasks are being started, tasks that are in background and must be unlinked to the current screen, because it can be closed before those tasks end, so they are initialized with the application context
that shouldn't be the problem
this worked perfectly and like a charm without compose
s
answer the dm I sent you man. It will make it much easier to figure out what is wrong
265 Views