Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将底部导航视图实现为一个片段_Android_Android Layout_Android Fragments - Fatal编程技术网

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

Android 将底部导航视图实现为一个片段,android,android-layout,android-fragments,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, savedIn

在我的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>


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

您正在尝试设置一个尚未创建的人。 因此,将代码移动到事件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);
        }

这将起作用:)

你能在这里发布与此
片段相关的
活动的布局吗。我用布局编辑了第一个问题有人能帮我吗?请:(