Get application context returns null

Create in onCreate() an instance of getApplicationContext() (mContext) then call MyApp.getContext() from everywhere in your app and you will get your application context statically.

public class MyApp extends Application {
    private static Context mContext;

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();    
    }
}

Remember to declare into your AndroidManifest.xml

<application android:name="com.mypackage.mypackage.MyApp">
...
...
...
</application>

Leave a Comment