java - 导航菜单 fragment 无法初始化其布局的组件

标签 java android android-layout android-fragments navigation-drawer

我已经为我的应用程序创建了一个导航菜单 fragment ,以便我可以在我的所有 Activity 中使用它。但是,当我运行该应用程序时,无论是单击汉堡包图标还是从左向右拉动时,菜单都不起作用。所以我决定使用调试器,我发现我的 fragment 的布局组件都没有在 fragment 的类中初始化。你能帮我弄清楚为什么会发生这种情况以及如何解决它吗?这是我的 fragment.java 类和我的 fragment_layout.xml 文件:

fragment .java:

public class NavMenuFragment extends Fragment {

// NavMenu member vars
private DrawerLayout mDrawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle mToggle; // Button for toggling the side menu

// Keeps the position of the previously selected menu item(0 : Home)
int position = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_nav_menu,null);
    mDrawerLayout = view.findViewById(R.id.drawerLayout);
    navigationView = view.findViewById(R.id.nav_view);
    mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button
    return view;
}

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

    // Sets the default selected menu item, to the Home item
    navigationView.getMenu().findItem(R.id.nav_home).setChecked(true);

    // Used to help on check and uncheck menu items when the user clicks on them
    final List<MenuItem> items = new ArrayList<>();
    Menu menu;
    menu = navigationView.getMenu();

    // Fill the list with all the menu items
    for(int i=0; i<menu.size();i++) {
        items.add(menu.getItem(i));
    }

    Toast.makeText(getActivity(), "size:" + items.size(), Toast.LENGTH_SHORT).show();

    // When an item inside the NavView gets clicked, then handle the event...
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.nav_home:
                    mDrawerLayout.closeDrawer(Gravity.START);
                    break;
                case R.id.nav_UserBoxGLB:
                    break;
                case R.id.nav_UserBoxJP:
                    break;
                case R.id.nav_settings:
                    break;
                case R.id.nav_feedback:
                    break;
                case R.id.nav_contact_us:
                    break;
                case R.id.nav_donate:
                    // Open the website's URL in a browser window
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_BROWSABLE);
                    intent.setData(Uri.parse("http://www.google.com"));
                    startActivity(intent);
                    break;
                case R.id.nav_about:
                    break;
                default:
                    return onNavigationItemSelected(item);
            }
            items.get(position).setChecked(false);
            item.setChecked(true);
            mDrawerLayout.closeDrawers();
            return false;
        }
    });

    mDrawerLayout.addDrawerListener(mToggle);
    // Set the hamburger icon's color
    mToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.NavActionBarTextColor));
    mToggle.syncState();
}

// When an item from the Action Bar gets tapped, then...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return mToggle.onOptionsItemSelected(item) || onOptionsItemSelected(item);
}
}

fragment 布局.xml:

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:id="@+id/drawerLayout">

    <!-- The actual side menu Nav View -->
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu"
        android:id="@+id/nav_view">

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

最佳答案

你做错了
如果你想对所有 fragment 使用相同的抽屉导航,那么
您可以在您的主要 Activity 中制作一个抽屉导航,并从该抽屉导航中打开 fragment 。
这是 tutorial

关于java - 导航菜单 fragment 无法初始化其布局的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470834/

相关文章:

java - android studio项目中没有菜单文件夹

android - 相对布局中的百分比宽度

java - 取消 SQL 字符串引号的方法

java - 插入二叉搜索树

iphone - 为什么移动设备必须定期检查服务器更新?

java - 应用程序在应用程序启动时开始搜索 GPS,而不是在需要时

java - 如何使用一个 SQL 查询从一个独立排序的表中合并两个结果集?

java - Spring Boot入门404错误

android - 方向更改时不会恢复 Fragment Backstack

android - TextView 不显示所有文本