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

java-导航菜单片段无法初始化其布局的组件,第1张

概述我为应用程序创建了导航菜单片段,以便可以在所有活动中使用它.但是,当我运行该应用程序时,单击汉堡图标或从左向右拖动时,菜单均不起作用.因此,我决定使用调试器,但发现片段的类中没有任何片段的布局组件初始化.您能帮我弄清楚为什么会这样以及如何解决吗?这是我的fragment.java类

我为应用程序创建了导航菜单片段,以便可以在所有活动中使用它.但是,当我运行该应用程序时,单击汉堡图标或从左向右拖动时,菜单均不起作用.因此,我决定使用调试器,但发现片段的类中没有任何片段的布局组件被初始化.您能帮我弄清楚为什么会这样以及如何解决吗?这是我的fragment.java类和fragment_layout.xml文件:

Fragment.java:

@H_403_6@public class NavMenuFragment extends Fragment {// NavMenu member varsprivate 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;@OverrIDepublic 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;}@OverrIDepublic 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...@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { return mToggle.onoptionsItemSelected(item) || onoptionsItemSelected(item);}}

Fragment_layout.xml:

@H_403_6@ <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>

解决方法:

你做错了
如果要对所有片段使用相同的导航抽屉,则
您可以在主活动中创建一个导航抽屉,然后从该导航抽屉中打开片段.
这是tutorial

总结

以上是内存溢出为你收集整理的java-导航菜单片段无法初始化其布局的组件全部内容,希望文章能够帮你解决java-导航菜单片段无法初始化其布局的组件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1081060.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存