Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference

My Application consist of 4 fragment as tabs being loaded inside a parent Fragment using FragmentPagerAdapter.

The problem is when I run the app and press back and re-open the app I receive this error log:

FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
    at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:1949)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1965)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
    at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:513)
    ...

the line of code inside parent Fragment is:

viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);

and ViewPage and Adapter both are not null!!

I have to mention that all my Fragments lifecycle is being managed and the null issue is happening inside the adapter!, and the same Adapter is working fine when I use an Activity as parent instead of Fragment!!!

like image 888
Mohsen Mirhoseini Avatar asked Jan 14 '17 13:01

Mohsen Mirhoseini


3 Answers

After a lot of research, it looks like it is a support library issue:

Activity with fragment and ViewPager crashesh on configuration change
Android Support Library issue 24.1.0

I also found this answer, but it looks unusual adding a flag to my code:

Attempt to invoke virtual method ‘android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()’ on a null object

I override the finishUpdate method of my FragmentPagerAdapter like this and problem solved for now!

@Override
public void finishUpdate(ViewGroup container) {
    try{
        super.finishUpdate(container);
    } catch (NullPointerException nullPointerException){
        System.out.println("Catch the NullPointerException in FragmentPagerAdapter.finishUpdate");
    }
}

any better answer? please let me know...

like image 64
Mohsen Mirhoseini Avatar answered Nov 09 '22 00:11

Mohsen Mirhoseini


I have the same issue on my project. But it is my fault.

WHY

I call onBackPressed in a callback after the activity has invoked onDestroy();

Because the mHost instance in the FragmentManager is set to null after onDestroy() called.

and when call onBackPressed it will use the mHost instance again.

like image 15
Liu Tom Avatar answered Nov 09 '22 02:11

Liu Tom


@Override
public void onBackPressed() {
//     super.onBackPressed();
       finish();
}  

Override the onBackPressed in activity..
remove the super call : super.onBackPressed
and call finish.

like image 7
varunjohn1990 Avatar answered Nov 09 '22 01:11

varunjohn1990