title: Using SearchView in ActionBar
body: <p>I'm trying to run a simple code that displays a <code>SearchView</code> when the user clicks the <code>Search</code> <code>action</code>.</p>
  
  <p>When I run the <code>application</code>, it closes with a <strong>FATAL EXCEPTION</strong>.</p>
  
  <p>My Code</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package luizugliano.com.br.customactionbar;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.SearchView;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
  
      MenuItem item = menu.findItem(R.id.action_search);
      SearchView searchView = (SearchView) item.getActionView();
      searchView.setOnQueryTextListener(onSearch());
      return true;
  }
  
  private SearchView.OnQueryTextListener onSearch() {
      return new SearchView.OnQueryTextListener(){
          @Override
          public boolean onQueryTextSubmit(String query) {
              // Usuário fez a busca
              toastMessage("Buscar o texto: " + query);
              return false;
          }
          @Override
          public boolean onQueryTextChange(String newText) {
              // Mudou o texto digitado
              return false;
          }
      };
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_search) {
          toastMessage("Search!");
          return true;
      } else if (id == R.id.action_refresh){
          toastMessage("Refresh!");
          return true;
      } else if (id == R.id.action_settings){
          toastMessage("Settings!");
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  private void toastMessage(String message){
      Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
  }
  }
  </code></pre>
  
  <p><strong>menu_main.xml</strong></p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"&gt;
  &lt;item android:id="@+id/action_search" android:title="@string/action_search"
      android:orderInCategory="100" android:icon="@drawable/ic_action_search" app:showAsAction="always"
      android:actionViewClass="android.widget.SearchView"/&gt;
  &lt;item android:id="@+id/action_refresh" android:title="@string/action_refresh"
      android:orderInCategory="100" android:icon="@drawable/ic_action_refresh" app:showAsAction="always" /&gt;
  &lt;item android:id="@+id/action_settings" android:title="@string/action_settings"
      android:orderInCategory="100" app:showAsAction="never" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>logcat information</strong></p>
  
  <blockquote>
    <p>10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar D/AndroidRuntime: Shutting down VM
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: <strong>FATAL EXCEPTION: main
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: Process: luizugliano.com.br.customactionbar, PID: 806
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference</strong>
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at luizugliano.com.br.customactionbar.MainActivity.onCreateOptionsMenu(MainActivity.java:29)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:243)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    10-20 00:30:21.327 806-806/luizugliano.com.br.customactionbar E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    10-20 00:30:21.686 806-820/luizugliano.com.br.customactionbar I/art: Background partial concurrent mark sweep GC freed 205(8KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 5MB/8MB, paused 41.133ms total 119.832ms
    10-20 00:30:35.570 806-806/? I/Process: Sending signal. PID: 806 SIG: 9</p>
  </blockquote>