简体   繁体   English

导航菜单片段无法初始化其布局的组件

[英]Navigation Menu Fragment not being able to initialize its layout's components

I've created a navigation menu fragment for my application so that i can use it across all of my activities. 我为我的应用程序创建了一个导航菜单片段,以便我可以在我的所有活动中使用它。 However, when i run the app the menu does not work neither when i click on the hamburger icon nor when i pull from left to right. 但是,当我运行应用程序时,菜单不会在我点击汉堡包图标时也不会工作,也不会在我从左向右拉动时。 So i decided to use the debugger and i found out that none of my fragment's layout components get initialized within the fragment's class. 所以我决定使用调试器,我发现我的片段的布局组件都没有在片段的类中初始化。 Can you help me figure out why this happens and how to fix it? 你能帮我弄清楚为什么会发生这种情况以及如何解决这个问题吗? Here is my fragment.java class and my fragment_layout.xml file: 这是我的fragment.java类和我的fragment_layout.xml文件:

Fragment.java: 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_layout.xml: Fragment_layout.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>

You are doing it wrong 你做错了
If you want to use same navigation drawer for all fragment then 如果你想为所有片段使用相同的导航抽屉
You can make a navigation drawer in your main activity and open fragment from that navigation drawer. 您可以在主活动中创建导航抽屉,并从该导航抽屉中打开片段。
Here is the tutorial 这是教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 片段布局未膨胀 - Fragment layout not being inflated 我无法使用片段从我的活动中访问片段布局内的文本视图 - I am not being able to access the text view inside my fragment layout from my Activity using the fragment 无法在其适配器Android App Development中调用Fragment的功能 - Not able to call the Fragment's function in its Adapter, Android App Development 如何将操作栏导航下拉菜单放置在主要详细信息布局中的第二个片段上方 - How to position actionbar navigation dropdown menu above second fragment in master detail layout 菜单片段类未被调用 - Menu fragment class not being called 使用导航架构组件启动带有结果的片段 - launch a fragment with result using navigation architecture components 在选择的导航项目上不显示带有选项卡布局的片段 - On navigation item selected not showing fragment with Tab Layout 带抽屉布局的底部导航 | 片段中的 onBackPressed - Bottom Navigation with Drawer Layout | onBackPressed in Fragment 嵌套视图的演示者未初始化且无法处理其视图的事件 - Nested View's Presenter not getting initialized and not being able to handle its view's events 导航菜单中的菜单布局未显示在导航抽屉中 - menu layout for the navigation menu is not showing up in the navigation drawer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM