Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Bottom Navigation View into a fragment

Tags:

In my Android Studio project I want to implement into the MainActivity a NavigationDrawer with fragments and on every fragments a bottom navigation view.

This is the code of one fragment:

public class U16 extends Fragment{

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("TITOLO U16");
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (item.getItemId()) {
            case R.id.navigation_home:
                fragmentTransaction.replace(R.id.content, new Calendario()).commit();
                return true;
            case R.id.navigation_dashboard:
                fragmentTransaction.replace(R.id.content, new Palestre()).commit();
                return true;
            case R.id.navigation_notifications:
                fragmentTransaction.replace(R.id.content, new Squadra()).commit();
                return true;
        }
        return false;
    }

};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    BottomNavigationView navigation = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content, new Calendario()).commit();

    return inflater.inflate(R.layout.u16_layout, container, false);
}

}

I get this error:

FATAL EXCEPTION: main
Process: app.navigationdrawerfragment, PID: 21499
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference at app.navigationdrawerfragment.U16.onCreateView(U16.java:52)

I know that this error in into the procedure "OnCreateView" doing " navigation.setOnNavigationItemSelectedListener..."

This is the u16_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="simone.biliato.navigationdrawerfragment.MainActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

So, anyone tried to do the same?

like image 667
Simone Avatar asked Oct 03 '17 18:10

Simone


People also ask

How do I create a new fragment and navigation drawer?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

What object is used to add a fragment to a layout?

To declaratively add a fragment to your activity layout's XML, use a FragmentContainerView element. The android:name attribute specifies the class name of the Fragment to instantiate.


1 Answers

You are trying do set someone that is isn't already created. So move the code into the event onViewCreated, like this:

     @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            getActivity().setTitle("Titolo");

BottomNavigationView navigation = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content, new Calendario()).commit();

    return inflater.inflate(R.layout.u16_layout, container, false);
        }

This will work :)

like image 191
img.simone Avatar answered Oct 11 '22 13:10

img.simone