片段布局组件为空
Fragment Layout components are null
我已经创建了一个 DrawerLayout(包含一个用于菜单的 NavView)片段并将其命名为 NavMenuFragment,并且我尝试将它实现到我的 mainActivity.java class 中,但没有成功。在 运行 调试器之后,我注意到片段布局文件中的所有布局都没有得到初始化,因此等于 null。关于为什么会发生这种情况的任何想法?这是片段中的 .java class 和 .xml 文件:
Fragment.java:
public class NavMenuFragment 扩展片段 {
// 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);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button
// 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"
xmlns:tools="http://schemas.android.com/tools"
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>
- RETURN 查看
- 传递 VIEW 对象而不是 getView().
FrameLayout
- 不需要。
试试这个方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_nav_menu, null);
mDrawerLayout = view.findViewById(R.id.drawerLayout);
..................................
navigationView = view.findViewById(R.id.nav_view);
return view;
}
我已经创建了一个 DrawerLayout(包含一个用于菜单的 NavView)片段并将其命名为 NavMenuFragment,并且我尝试将它实现到我的 mainActivity.java class 中,但没有成功。在 运行 调试器之后,我注意到片段布局文件中的所有布局都没有得到初始化,因此等于 null。关于为什么会发生这种情况的任何想法?这是片段中的 .java class 和 .xml 文件:
Fragment.java:
public class NavMenuFragment 扩展片段 {
// 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);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button
// 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"
xmlns:tools="http://schemas.android.com/tools"
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>
- RETURN 查看
- 传递 VIEW 对象而不是 getView().
FrameLayout
- 不需要。
试试这个方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_nav_menu, null);
mDrawerLayout = view.findViewById(R.id.drawerLayout);
..................................
navigationView = view.findViewById(R.id.nav_view);
return view;
}