title: where to add setOnQueryTextListener for search view to filter the list?
body: <p>I am confused about where to add the search view listener as I have a list in fragment. I have declared all the variables of related to list as static. Adapter also declared as a static variable in main activity. But when I set the onQueryTextListener in onCreateOptionsMenu method of main activity I get the null pointer exception on adapter. So I thought of adding the listener in a fragment as the adapter is in fragment. So I declared searchview variable as a static variable so that i can access it in fragment but it failed as I get the null pointer exception on searchview. Can anybody guide me for this please.. </p>
  
  <p>Main Activity</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_main, menu);
  
      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView= (SearchView) menu.findItem(R.id.menu_search).getActionView();
      searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );
  
      return true;
  }
  </code></pre>
  
  <p>Fragment code</p>
  
  <pre><code>public class ViewStock extends Fragment {
      final MainActivity act = (MainActivity) this.getActivity();
      public RecyclerView mRecyclerView;
  
      public ViewStock() {
          // Required empty public constructor
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View rootview = inflater.inflate(R.layout.fragment_view_stock, container, false);
          act.array_data = new ArrayList&lt;Item&gt;();
  
          mRecyclerView = (RecyclerView)rootview.findViewById(R.id.my_recycler_view);
          LinearLayoutManager llm = new LinearLayoutManager(getActivity());
          llm.setOrientation(LinearLayoutManager.VERTICAL);
          mRecyclerView.setLayoutManager(llm);
  
          act.db = new DBHandler(getActivity());
          ArrayList&lt;Item&gt; item_array_from_db = act.db.Get_items();
  
          for (int i = 0; i &lt; item_array_from_db.size(); i++) {
  
              int idno = item_array_from_db.get(i).getID();
              String name = item_array_from_db.get(i).getItem_name();
              String desc = item_array_from_db.get(i).getItem_desc();
              String qty = item_array_from_db.get(i).getItem_qty();
              Item cnt = new Item();
              cnt.setID(idno);
              cnt.setItem_name(name);
              cnt.setItem_desc(desc);
              cnt.setItem_qty(qty);
  
              act.array_data.add(cnt);
          }
          act.db.close();
          act.adapter=new ItemAdapter(getActivity(),act.array_data);
          mRecyclerView.setAdapter(act.adapter);
  
          return rootview;
      }
  }
  </code></pre>
  
  <p>searchview activity</p>
  
  <pre><code>    public class SearchResultsActivity extends Activity{
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          handleIntent(getIntent());
      }
  
      @Override
      protected void onNewIntent(Intent intent) {
          super.onNewIntent(intent);
  
          handleIntent(intent);
      }
  
      private void handleIntent(Intent intent) {
  
          if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
              String query = intent.getStringExtra(SearchManager.QUERY);
  
              showResults(query);
          }
      }
  
      private void showResults(String query) {
          // Query your data set and show results
          // ...
      }
  }
  </code></pre>
  
  <p>I am not getting any result by doing this. Please help..</p>