Attempt to invoke virtual method 'android.os.Handler' on a null object reference-ThrowExceptions

Exception or error:

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:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Handler' on a null object reference

the line of code inside parent Fragment is:


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!!!

How to solve:

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

Activity with fragment and ViewPager crashesh on configuration

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’ on a null object

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

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

any better answer? please let me know…


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


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.


I know it might be late, but here’s my stupid mistake I didn’t see straight away:

public void onStop() {
    super.onDestroy(); //I was calling the wrong super


I had the same issue and it was caused by the reuse of ViewPager instance by different instances of a fragment. A solution was to null the adapter in onDestroyView() method of the fragment.

@Override public void onDestroyView() {

Project demoing the issue and the solution can be found here.


I had the same issue. In my case the problem was that I was re-using without noticing the adapter from the previous time opened.

I looked into the class that send the exception and on the very same line that crashes says:
“Must be called from main thread of fragment host”

my code was:

PagerAdapter adapter = MPageAdapter.getInstance(getSupportFragmentManager()); 

because I was using my PageAdapter as a bridge to communicate between fragments I used a singleton pattern. That worked well for me but once I repeated this structure in another activity I got the same exception. And the reason was that I forgot to destroy the singleton of my adapter class at the end of every use.

protected void onDestroy() {
    MPageAdapter.Destroy();  //I forgot this one, that makes singleton = null;


public void onBackPressed() {
//     super.onBackPressed();

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


I was using the wrong activity reference to fetch


Using the correct activity fixed the crash for me.

Leave a Reply

Your email address will not be published. Required fields are marked *