0

Im trying to hide a Bottom Navigation while scrolling ( inside a Fragment ) .

Im using this code snippet for the hide show / hide functionality which I found here https://stackoverflow.com/a/44779186/8611488 :

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            if (dy > 0 ||dy<0 && bottomNav.isShown())
            {
                bottomNav.setVisibility(View.GONE);
            }
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState)
        {
            if (newState == RecyclerView.SCROLL_STATE_IDLE)
            {
                bottomNav.setVisibility(View.VISIBLE);
            }

            super.onScrollStateChanged(recyclerView, newState);
        }
    });

I have declared my Bottomnavigation in Fragment like so :

BottomNavigationView mBottomNav = rootview.findViewById(R.id.bottom_navigation);

so the Fragment has acces to the bottomnav variable from Mainactivity -->

BottomNavigationView mBottomNav = findViewById(R.id.bottom_navigation);

app just crashes on scrolling.

Logcat crash :

Attempt to invoke virtual method 'boolean android.support.design.widget.BottomNavigationView.isShown()' on a null object reference – 

Any help is appriacted

EDIT :

This is my Fragment Class(the relevant stuff) :

public class SearchFragment extends Fragment implements SearchItemAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private SearchItemAdapter mExampleAdapter;
    private ArrayList<SearchItem> mExampleList;
    private RequestQueue mRequestQueue;


    private BottomNavigationView mBottomNav ;


    public static final String EXTRA_URL = "imageUrl";
    public static final String EXTRA_CREATOR = "creatorName";
    public static final String EXTRA_LIKES = "likeCount";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_search, container, false);


        EditText editText = rootView.findViewById(R.id.edittext);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                filter(s.toString());
            }
        });


      mBottomNav = rootView.findViewById(R.id.bottom_navigation);

        mRecyclerView = rootView.findViewById(R.id.recycler_view);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mExampleList = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(getActivity());
        parseJSON();







        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0 && mBottomNav.isShown()) { // <<mBottomNav causes crash ( why is it null?)
                    mBottomNav.setVisibility(View.GONE);
//                    Toast.makeText(getActivity(),"WORKS ",Toast.LENGTH_SHORT).show();
                } else if (dy < 0 ) {
                    mBottomNav.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
            }
        });


        return rootView;
    }
Medy
  • 55
  • 1
  • 6
  • Add your logcat please. – Aditya Mar 02 '18 at 17:51
  • 03-02 17:36:02.523 17109-17109/com.gamelist.gamelist E/AndroidRuntime: FATAL EXCEPTION: main Process: com.gamelist.gamelist, PID: 17109 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.design.widget.BottomNavigationView.isShown()' on a null object reference – Medy Mar 02 '18 at 19:29
  • Is this logcat from fragment or activity ?. but either this is from activity or fragment, you are calling `isShown()` method on a null view. Cross-chec your code where are you using `isShown()`. – Aditya Mar 03 '18 at 04:47
  • I added the relavent code to my post. logcat is from the fragment – Medy Mar 03 '18 at 14:41
  • It should be clear now that the mbottomNav ( is pointing to a nulll object) ... I have no idea why it is null though. ;( – Medy Mar 03 '18 at 14:45

0 Answers0