title: Why does getActionView() return null
body: <p>I've been searching for a solution for this for hours now. I just want to add a switch under the ActionBar (like in Bluetooth settings). I found a similar question on here, but it was probably old. Anyways here's my code:</p>
  
  <p>MainActivity:</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_main, menu);
  
      MenuItem item = menu.findItem(R.id.myswitch);
      switchButton = (Switch) item.getActionView();
  
      return super.onCreateOptionsMenu(menu);
  }
  </code></pre>
  
  <p>menu_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"   &gt;
  
  &lt;item
      android:id="@+id/menu_switch"
      android:title="off/on"
      app:showAsAction="always"
      app:actionLayout="@layout/switchlayout"
      app:actionViewClass="android.support.v7.widget.Switch" /&gt;
  
  &lt;/menu&gt;
  </code></pre>
  
  <p>switchlayout.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"&gt;
  
      &lt;Switch
          android:id="@+id/myswitch"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:background="#1E88E5" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>But no matter what I do I always get:</p>
  
  <blockquote>
    <p>Attempt to invode ... getActionView()' on a null object reference</p>
  </blockquote>
  
  <p>I'm confused because i just defined item the line before <code>R.id.myswitch</code> is defined, did I mess that up? </p>