Skip to content Skip to sidebar Skip to footer

Setuservisiblehint() Throwing Nullpointerexception When Calling Getactivity()

I have create 3 Fragments which are in a ViewPager. I am trying to get data from the server only when the particular Fragment is visible to the user. For that I have called my web

Solution 1:

Keep a Activity reference when onAttach() is called and use it everywhere instead of getActivity() Like this:

@OverridepublicvoidonAttach(Activity activity) {
            super.onAttach(activity);
            mActivity = activity;
            }

Solution 2:

try this :

boolean isVisible=false;
@OverridepublicvoidsetUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    isVisible=isVisibleToUser;

}

 @OverridepublicvoidonAttach(Context context) {
    if(visible){
    // do what you must todo
    }else{
     //do what you must todo
    }

}

Post a Comment for "Setuservisiblehint() Throwing Nullpointerexception When Calling Getactivity()"