-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),即将到来的事件和过去的事件,我可以在每个标签之间滑动没问题。而联系人只是一个显示用户信息的简单布局。

直到最近我将 searchView 添加到事件页面之前,此代码一直运行良好,没有任何问题。

searchView添加到EventDashboard片段中onCreateOptionMenu

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);
    }

    ...
}

在这两个片段中,我都有以下代码EventTabUpComingEventTabPast

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

现在,问题是一旦我滑动到EventDashboard片段中的不同选项卡,然后从抽屉中选择联系人,它现在会抛出异常,如下所示,我不明白,而当前片段被所选片段替换,为什么它试图onCreateOptionMenu从前一个片段调用。奇怪的是,如果我不滑动到EventDashboard片段中的不同选项卡,切换到联系人片段将没有问题。

这是一个例外:

2019-02-26 22:49:19.286 21132-21132/com.myapp.event I/MyApp.MainActivity: Displaying selected screen id: 2131362023
2019-02-26 22:49:19.286 21132-21132/com.myapp.event D/MyApp.MainActivity: Displaying selectedScreen with id 2131362023
2019-02-26 22:49:19.293 21132-21132/com.myapp.event D/MyApp.MainActivity: showing Contact screen Contacts{8d6ec57}
2019-02-26 22:49:19.400 21132-21132/com.myapp.event E/MyApp.Contacts: loading user list and user lookup for the list
2019-02-26 22:49:19.412 21132-21209/com.myapp.event E/MyApp.UserListsRepository: getUsersForList Christmas
2019-02-26 22:49:19.412 21132-21209/com.myapp.event E/MyApp.UserListsLocalDataSource: getUsersForList Christmas
2019-02-26 22:49:19.417 21132-21132/com.myapp.event D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-02-26 22:49:19.417 21132-21132/com.myapp.event E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp.event, PID: 21132
    java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference
        at android.support.v4.view.MenuItemCompat.getActionView(MenuItemCompat.java:188)
        at com.myapp.event.Fragments.Events.EventTabPast.onCreateOptionsMenu(EventTabPast.java:94)
        at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2547)
        at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:3319)
        at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:331)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:369)
        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
        at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2546)
        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
        at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:455)
        at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:56)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:655)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

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

@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);
}
4

1 回答 1

0

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

您在联系人片段的 onResume() 中编写以下代码

@Override
public void onResume()
{
    super.onResume();
    setHasOptionsMenu(false)
}
于 2019-02-27T05:25:51.453 回答