-4

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?

Nissa
  • 4,636
  • 8
  • 29
  • 37
stack Learner
  • 1,318
  • 3
  • 18
  • 35
  • 4
    seems like an error in WhereIsTheCode.java line 666 – Selvin Oct 03 '16 at 11:04
  • You need to write at lease some portion of code of your fragment manager. Especially the line where the error occured – O_o Oct 03 '16 at 11:07
  • It is crashing in internal files.Reason is setRetainInstance(true).If i remove it,it is not crashing – stack Learner Oct 03 '16 at 11:08
  • Something in your code is handling fragment lifecycle incorrectly. WIthout any crystal balls it's not really possible to say what the problem is specifically. Full stack traces help, too. – laalto Oct 03 '16 at 11:20
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Nissa Oct 03 '16 at 23:54

3 Answers3

1

You just need to check if Fragment has been attached or not by using this

if (isAdded()) {
 //do your stuff
}
TranHieu
  • 914
  • 6
  • 23
0

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.

Community
  • 1
  • 1
dipali
  • 10,966
  • 5
  • 25
  • 51
0

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
        }
Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38