Adding SearchView in Fragment

前端 未结 7 724
误落风尘
误落风尘 2020-12-14 02:36

Trying to include a searchview in actionbar. For this, I have done the following:

Created MenuSearch.xml in the menu folder as given below:



        
7条回答
  •  轮回少年
    2020-12-14 03:05

    i solved this issue i wants to share my answer for searchView NullPointerException

    we need to remove below method from Main Activity

          public boolean onOptionsItemSelected(MenuItem item) {
              int id = item.getItemId();  
              return true;
    

    and use inside fragment in OnActivityCreated setHasOptionsMenu(true) and Override methods

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
             inflater.inflate(R.menu.menu, menu);
        }
    
        @Override
         public boolean onOptionsItemSelected(MenuItem item) {
           // handle item selection
            switch (item.getItemId()) {
                case R.id.action_search:
    
         //       onCall();   //your logic
    
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

提交回复
热议问题