Java 如何替换OnOptions ItemSelected中的片段?

Java 如何替换OnOptions ItemSelected中的片段?,java,android,android-fragments,Java,Android,Android Fragments,我有一个AppCompatActivity,它是应用程序的主视图。 在这里,我有一个OptionsMenu,其中一个按钮显示抽屉菜单,另一个按钮过滤结果(因为抽屉中的每个菜单项都是不同的ListFragment) 我需要完成的是,当单击Options菜单上的filter按钮时,打开一个包含所有筛选条件的片段 这是管理上述内容的代码: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.g

我有一个AppCompatActivity,它是应用程序的主视图。 在这里,我有一个OptionsMenu,其中一个按钮显示抽屉菜单,另一个按钮过滤结果(因为抽屉中的每个菜单项都是不同的ListFragment)

我需要完成的是,当单击Options菜单上的filter按钮时,打开一个包含所有筛选条件的片段

这是管理上述内容的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_menu:

            if (drawer.isDrawerOpen(GravityCompat.START)) {
                hideDrawer();
            } else {
                showDrawer();
            }

            return true;
        case R.id.action_filter:

            fragmentManager = getFragmentManager();

            fragment = new FilterFragment();
            args.putInt("idList", idList);
            fragment.setArguments(args);

            fragmentManager.beginTransaction().replace(R.id.drawer_layout, fragment).commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
这样,片段就会出现,然后我得到一个NPE

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TableRow.setVisibility(int)”

片段onCreateViewMethod

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();
    skbrDays = getActivity().findViewById(R.id.seekBar);
    daysLostRow = getActivity().findViewById(R.id.daysLostRow);
    genderRow = getActivity().findViewById(R.id.genderRow);
    locationRow = getActivity().findViewById(R.id.locationRow);
    rgupGender = getActivity().findViewById(R.id.rgupGender);
    rgupType = getActivity().findViewById(R.id.rgupType);
    btnBreed = getActivity().findViewById(R.id.btnBreed);
    recyclerView = getActivity().findViewById(R.id.recyclerView);

    daysLostRow.setVisibility(View.VISIBLE);

    return inflater.inflate(R.layout.activity_filter, container, false);
}
我得到NPE是因为getActivity()返回null。现在我不得不猜测,不知道原因,因为我没有替换活动中onCreate方法上的片段


如果这就是我如何从onCreate调用片段来获得相同结果的原因?

将onCreateView中的代码替换为下面的代码

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.activity_filter, container, false);
    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();
    skbrDays = rootView.findViewById(R.id.seekBar);
    daysLostRow = rootView.findViewById(R.id.daysLostRow);
    genderRow = rootView.findViewById(R.id.genderRow);
    locationRow = rootView.findViewById(R.id.locationRow);
    rgupGender = rootView.findViewById(R.id.rgupGender);
    rgupType = rootView.findViewById(R.id.rgupType);
    btnBreed = rootView.findViewById(R.id.btnBreed);
    recyclerView = rootView.findViewById(R.id.recyclerView);

    daysLostRow.setVisibility(View.VISIBLE);

    return rootView;
}

它不是
getActivity()
。它是findViewById(R.id.dayslow)。您可能希望扔掉该片段代码并重新开始:片段通常不应该依赖于使用它们的活动的类型。@M.Prokhorov但是findViewById()不是Fragmentyes继承的方法,但是您的异常也不会说“试图在空对象引用上调用findViewById”,这意味着
getActivity()
返回某些内容。但是,
setVisibility
在您显示的代码中只调用一次,这意味着
dayslow
null
。你认为那个变量来自哪里?@M.Prokhorov你是对的getActivity()没有返回null。DaysLow是以前定义的全局变量。但问题不在于这个特定的变量,我在任何其他视图上调用的任何方法都会给我一个NPE,这是因为它是
null
。很可能是因为原始活动的视图中没有任何带有
R.id.dayslow
的元素。谢谢!这就成功了。你能解释一下为什么我所做的不起作用,而你的代码起作用了吗?虽然这是一种修复代码的潜在方法,但没有解释它为什么起作用,这不是一个好的答案。