在我的mainActivity
中,我定义了一个抽屉,并将mainActivity
约束布局替换为抽屉中的选定项(作为Fragment)。
@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);
}
...
}
在EventTabUpComing
和EventTabPast
片段中,我都有以下代码:
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
片段中滑动到其他选项卡,则切换到Contact片段将没有问题。
这里是例外:
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已经加载了新片段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);
}
答案 0 :(得分:0)
每当在片段中写入onCreateOptionMenu()时,这意味着它在Fragment的父活动上创建
您在Contact Fragment的onResume()中编写以下代码
@Override
public void onResume()
{
super.onResume();
setHasOptionsMenu(false)
}