I have my app having view pager and fragments. In my parent parent fragment I have made setRetainInstance(true)
.
I put my app in background and change the font size. When I open my app again, it crashes.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:667)
I cannot use setRetainInstance(false)
. As I need to retain the state. How do I fix it?
You just need to check if Fragment has been attached or not by using this
if (isAdded()) {
//do your stuff
}
Case 1:
Use setRetainInstance DO NOT keep your view in memory just keep same instance as a kind of Singleton based on tag or ID that you give to your fragment. It's good because when you Fragment goes to background your view is destroyed and not unecessary memory.
Check this link. Reference Link
case 2:
Your error is not generating when font size has changed.but your arraylist.size getting null .please check yourfile.java in line 666.please check condition if arraylist != null whenever use arraylist.size() in your code.
These is because you accessed an empty array
add below code before you access am array list
if ( !nArrayList.isEmpty() ||nArrayList != null) {
//access your arraylist
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.