Q:

Search in a ListView results in a null object reference

I'm going through these API Guides: https://developer.android.com/guide/topics/search/search-dialog.html
and willing to implement searching in my simple ListView. I just don't know why i'm getting this: 

Attempt to invoke virtual method 'void >android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQuer>yTextListener)' on a null object reference

This is my code. Please help me to figure this out:
MenuItem ourSearchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) ourSearchItem.getActionView();

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }
});

A:

Set actionViewClass attribute in your menu item XML like
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/ic_search"
          android:showAsAction="collapseActionView|ifRoom"
          app:actionViewClass="android.widget.SearchView" />
</menu>

or if you use support library:
 app:actionViewClass="android.support.v7.widget.SearchView"

