Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add a SearchWidget to the ActionBar?
    primarykey
    data
    text
    <p>I'm trying to add a Search-ActionView to my application (as explained here <a href="http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget" rel="noreferrer">http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget</a>). Unfortunately I keep getting a NullPointerException and I'm having a hard time detecting what's actually going wrong.</p> <p>I created a searchable config and a searchable activity as shown on the android page. My menu .xml file looks like this:</p> <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt; ... &lt;item android:id="@+id/menu_item_search" android:actionViewClass="android.widget.SearchView" android:icon="@drawable/icon_search" android:showAsAction="always" android:title="@string/action_bar_button_search"&gt; &lt;/item&gt; &lt;/menu&gt; </code></pre> <p>This is the method where the Exception is thrown:</p> <pre><code>public boolean onCreateOptionsMenu( Menu menu ) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate( R.menu.action_bar, menu ); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView(); // NullPointerException thrown here; searchView is null. searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false); return super.onCreateOptionsMenu( menu ); } </code></pre> <p>Complete stack trace:</p> <pre><code>FATAL EXCEPTION: main java.lang.NullPointerException at com.example.activities.Test.onCreateOptionsMenu(Test.java:41) at android.app.Activity.onCreatePanelMenu(Activity.java:2444) at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:408) at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:759) at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2997) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4507) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload