将底部导航视图实现为片段

时间:2017-10-03 18:32:23

标签: android android-layout android-fragments

在我的Android Studio项目中,我想在MainActivity中实现一个带有片段的NavigationDrawer,并在每个片段上实现一个底部导航视图。

这是一个片段的代码:

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);
}

}

我收到此错误:

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)

我知道这个错误进入“OnCreateView”程序“navigation.setOnNavigationItemSelectedListener ...”

这是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>

那么,有人试过这样做吗?

1 个答案:

答案 0 :(得分:2)

您正在尝试设置尚未创建的人。 所以将代码移到onViewCreated事件中,如下所示:

     @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);
        }

这将有效:)