我有一个MainActivity,其中放置了Fragment。具有SearchView的片段(app:actionViewClass =" android.support.v7.widget.SearchView" )和MainActivity在工具栏中有一个SearchView图标。 我想隐藏/显示片段的Searchview是显示/隐藏时的工具栏中的图标(如Flipkart应用程序)。当使用Hiding或Showing方法之一时,它正常工作,但当我同时使用它们时,它会给出错误,如
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference
我的代码段是......
MainActivity
@Override
public boolean onCreateOptionsMenu(final Menu menu)
{
_menu = menu;
final Menu m= menu;
getMenuInflater().inflate(R.menu.menus, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) DrawerActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m.performIdentifierAction(searchItem.getItemId(), 0);
}
});
searchItem.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
public void show_search(){
final MenuItem searchItem = _menu.findItem(R.id.action_search);
searchItem.setVisible(true);
// invalidateOptionsMenu(); //if i use this it not show the icon.
}
public void hide_search() {
final MenuItem sitem = _menu.findItem(R.id.action_search); // gives NullPointerException in this line.
sitem.setVisible(false);
invalidateOptionsMenu();
}
片段代码:
public class Fragment1 extends Fragment
{
scroll = (ScrollView) v.findViewById(R.id.scroll_view);
// Total Height of layout which contains searchview
ViewTreeObserver vas = lin.getViewTreeObserver();
vas.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewGroup.MarginLayoutParams vlp = (ViewGroup.MarginLayoutParams) lin.getLayoutParams();
search_size = lin.getMeasuredHeight() + vlp.topMargin;
lin.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
float y = scroll.getScrollY();
DrawerActivity d = (DrawerActivity) getActivity();
if (y >= search_size)
{
d.show_search();
}
else
{
d.hide_search();
}
}
});
}
答案 0 :(得分:0)
早上好,你为什么不膨胀其他menu.xml片段? 例如..在片段插入
中@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
}
private Menu mMenu;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragment, menu);
mMenu = menu;
MenuItem searchViewItem = menu.findItem(R.id.search);
final android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchViewItem);
super.onCreateOptionsMenu(menu, inflater);
}
我帮助了