Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

App is crashing in LocalizationApplication getApplicationContext() #95

Closed
ashutoshgohil opened this issue Mar 17, 2021 · 5 comments
Closed

Comments

@ashutoshgohil
Copy link

ashutoshgohil commented Mar 17, 2021

Hi found below issue in some devices, can you please help me to get it resolve. I have mentioned error below

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:118)
at com.LocalizationApplication.getApplicationContext(LocalizationApplication.java:23)

@akexorcist
Copy link
Owner

Hi @ashutoshgohil

Where did you put the getApplicationContext() in your code?

@ashutoshgohil
Copy link
Author

abstract class LocalizationApplication : Application() {
    private val localizationDelegate = LocalizationApplicationDelegate()

    override fun attachBaseContext(base: Context) {
        localizationDelegate.setDefaultLanguage(base, getDefaultLanguage())
        super.attachBaseContext(localizationDelegate.attachBaseContext(base))
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        localizationDelegate.onConfigurationChanged(this)
    }

    override fun getApplicationContext(): Context {
        return localizationDelegate.getApplicationContext(super.getApplicationContext())
    }

    abstract fun getDefaultLanguage(): Locale
}

@akexorcist
Copy link
Owner

akexorcist commented Apr 5, 2021

@ashutoshgohil Is there any more getApplicationContext() invoke in your code?. And could you provide the full stack trace. 3 lines of stack trace doesn't enough to determine the cause of problem.

@akexorcist
Copy link
Owner

@ashutoshgohil 1.2.8 in coming, please check it out. it might solve this problem (Refer to #97).

@akexorcist
Copy link
Owner

akexorcist commented Apr 12, 2021

@ashutoshgohil It's my fault. I forget to put the override fun getResources(): Resources in README.md

For the application delegation you need to add getResources(): Resources as override method as below

// LocalizationApplication.kt
override fun getResources(): Resources {
    return localizationDelegate.getResources(this)
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants