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>
Why am I getting getApplicationcontext() null?
In the constructor it is way too early to access the app context. Try to move this code into the onCreate
method.
More datails about the life cycle can be found in the documentation
getApplicationContext() returns null in Application class
You must use static
public class Shopper extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
And use
Shopper.getContext();
getApplicationContext() returns null in a Service
You are running your Service
in a different process so it has no communication with the Actual Application process..
Try to remove this line from your service
android:process=":pointservice"
After your application is finished Application context is becomes null. At most situations there is no need to run the service in a different process. Also it is not recommended.
For more information check here
- Service in another process
- service reference Example2
Spring application context is null
I think you need specify spring to set autowire these beans. For example
@Autowired
private ApplicationContext springContainer;
Another suggestion is use ApplicationContextProvider which implements springs ApplicationContextAware interface.
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;
public static ApplicationContext getApplicationContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext ctx) {
context = ctx;
}
}
You can get the application context via ApplicationContextProvider.getApplicationContext();
Why getContext() in fragment sometimes returns null?
First of all, as you can see on this link, the method onCreateView() inside the fragment's lifecycle comes after onAttach(), so you should have already a context at that point. You may wonder, why does getContext() return null then? the problem lies on where you are creating your adapter:
App.getApiService().getLatestNews().enqueue(new Callback<LatestNews>() {
@Override
public void onResponse(Call<LatestNews> call, Response<LatestNews> response) {
if (response.isSuccessful() && response.body().isSuccessfull()){
adapter = new LastNewsRVAdapter(getContext(), response.body().getData(), sideBanner);
rvLatestNews.setAdapter(adapter);
tvSonkuKabar.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(Call<LatestNews> call, Throwable t) {
}
});
Though you are specifying a callback in onCreateView(), that does not mean the code inside that callback will run at that point. It will run and create the adapter after the network call is done. With that in mind, your callback may run after that point in the lifecycle of the fragment. What I mean is that the user can enter that screen (fragment) and go to another fragment or return to the previous one before the network request finishes (and the callback runs). If that happens, then getContext() could return null if the user leaves the fragment (onDetach() may have been called).
Besides, you can have memory leaks also, in case the activity is destroyed before your network request finishes. So you have two issues there.
My suggestions to solve those issues are:
in order to avoid the null pointer exception and the memory leak, you should cancel the network request when the onDestroyView() inside the fragment is being called (retrofit returns an object that can cancel the request: link).
Another option that will prevent the null pointer exception is to move the creation of the adapter LastNewsRVAdapter outside the callback and keep a reference to it in the fragment. Then, use that reference inside the callback to update the content of the adapter: link
Related Topics
Apk Location in New Android Studio
Using Android Intent.Action_Send for Sending Email
How to Hide System Navigation Bar in Tablets
Get Listview Item Position on Button Click
Importing Module in Android Studio
How to Get App's Permission for Each App? How to Do It Programmatically on Android
Play Sound on Button Click Android
Findclass from Any Thread in Android Jni
Android Edittext Onchange Listener
How to Programmatically Enable/Disable Accessibility Service in Android
Android: How to Share Image with Text on Facebook via Intent
Android: How to Make an Activity Return Results to the Activity Which Calls It
Why Does Alertdialog.Builder(Context Context) Only Accepts Activity as a Parameter
How to Pass Bundle from Fragment to Fragment