I have switched my application to a MultiDexApplication by following the instructions in this link - https://developer.android.com/studio/build/multidex.html.
The application now installs correctly, BUT my application context is null. Have any of you experienced anything like this before?
Let me know if you require code samples, but since the app crashes because it cannot get the application context, I cant think of any code that would explain the issue.
Getting my application context as follows:
TrackerApplication.getContext();
public class TrackerApplication extends MultiDexApplication {
private static Context context;
public TrackerApplication() {
super();
context = this;
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
}
public static Context getContext() {
return context;
}
}
Then when I call:
TrackerApplication.getContext().getSharedPreferences("MySettings",
Context.MODE_PRIVATE);
I get a NullPointer. My App context is null.
Added a test to explain the issue. In my Launch activity I added this code:
if (TrackerApplication.getContext() == null) {
Log.i("TEST", "Context is null");
} else {
Log.i("TEST", "What is the issue?");
}
LogCat always prints out Context is null
.
Ended up fixing this by doing the following: 1- Completely removed multidex code from app. 2- Cleaned project. 3- Built project. 4- Added all the multidex code into the project again. 5- Cleaned and rebuilt the project again. 6- It just started working after that..
Don't know why it didn't work the first time, but now it works fine.