加载新片段时会调用上一个片段onCreateOptionsMenu

问题描述 投票:-1回答:1

在我的mainActivity中,我有一个抽屉定义,将用抽屉中的选定项目替换mainActivity约束布局(作为片段)。

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    /**
     * Perform logout logic if logout is selected, otherwise go display corresponding screen
     */
    if (id == R.id.nav_logout) {
        logout(null, true);
    } else {
        switch(id) {
            case R.id.nav_event:
                fragment = new EventDashboard();
                Log.d(logTag, "showing Event screen "  + fragment.toString());
                fragmentTag = "event";
                break;
            case R.id.nav_contacts:
                fragment = new Contacts();
                Log.d(logTag, "showing Contact screen "  + fragment.toString());
                fragmentTag = "contacts";
                break;
            }

        if (fragment != null) {
            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_main, fragment, fragmentTag);
            ft.commit();
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
    }
    return true;
}

此EventDashboard片段中有两个选项卡(SectionPagerAdapter),UpComing事件和过去事件,我可以在每个选项卡之间滑动没问题。联系人只是一个显示用户信息的简单布局。

这段代码一直运行良好,直到最近我才将searchView添加到事件页面。

通过qazxsw poi将qazxsw poi添加到qazxsw poi片段中

searchView

EventDashboardonCreateOptionMenu片段中,我有以下代码:

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

        /**
         * Enable option menu
         */
        setHasOptionsMenu(true);

        return inflater.inflate(R.layout.fragment_event, container, false);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //super.onCreateOptionsMenu(menu, inflater);
        /**
         * Inflate menu
         */
        inflater.inflate(R.menu.event_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    ...
}

EventTabUpComing的代码非常相似,只是它显示的数据不同。搜索按钮工作得很好,我可以在每个选项卡中过滤任何事件没有问题。

现在,问题是我一扫到EventTabPast片段中的不同选项卡,然后从抽屉中选择Contact,它现在抛出异常,如下所示,我不明白,而当前片段被所选片段替换,为什么它试图从之前的片段调用public class EventTabUpcoming extends Fragment implements SearchView.OnQueryTextListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.event_tab_upcoming, container, false); Log.d(logTag, "Showing Up-Coming events"); sectionAdapter = new SectionedRecyclerViewAdapter(); Log.i(logTag, "create recyclerView"); RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listUpcomingEvent); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL)); recyclerView.setAdapter(sectionAdapter); setHasOptionsMenu(true); return view; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { /** * Inflate menu */ MenuItem item = menu.findItem(R.id.action_event_search); searchView = (SearchView) MenuItemCompat.getActionView(item); searchView.setOnQueryTextListener(this); } } 。奇怪的是,如果我不滑动到EventTabPast片段中的不同选项卡,切换到Contact片段将没有问题。

这是一个例外:

EventDashboard

如跟踪所示,mainActivity已经加载了新的片段Contact,但由于某种原因,它还尝试调用onCreateOptionMenu。有问题的行是EventTabPast.java:94,它是“searchView =(SearchView)MenuItemCompat.getActionView(item);”

onCreateOptionMenu
java android fragment searchview sectionedrecyclerviewadapter
1个回答
0
投票

每当在片段中写入onCreateOptionsMenu()时,它意味着它在Fragment的父活动上创建

您在Contact Fragment的onResume()中编写以下代码

EventDashboard
© www.soinside.com 2019 - 2024. All rights reserved.