0

I am trying to create list search view in shreyaloclistfragment. I am getting NullPointerException at folowing line

final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

My method is

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate( R.menu.menu_search, menu);
    System.out.println( "inflating menu");

    final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();



    final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            showFilteredItems( newText );
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            return true;
        }
    };

    searchView.setOnQueryTextListener(queryTextListener);

    return;
}

Stack trace

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.sears.syw.FragmentShopsTab.onCreateOptionsMenu(FragmentShopsTab.java:52)
at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
at  


com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559    )
at   

    com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSh    erlockNative.java:65)
at 
Vivek Thummar
  • 395
  • 4
  • 17
Sanjeev Yadav
  • 2,921
  • 7
  • 24
  • 23

6 Answers6

9

If you are using the Appcompat library, maybe you forgot to add your own xmlns on your menu_search.xml.

As the example says:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      yourapp:showAsAction="ifRoom"  />
</menu>

You need to add the folowing:

<item android:id="+@id/..."
      android:actionViewClass="android.widget.SearchView"
      yourapp:actionViewClass="android.widget.SearchView" />

Source: https://developer.android.com/training/basics/actionbar/adding-buttons.html#XML

Xelz
  • 1,059
  • 2
  • 11
  • 16
4

If you are using AppCompat library, then in your menu.xml, add

app:actionViewClass="android.support.v7.widget.SearchView"

instead of

app:actionViewClass="android.widget.SearchView"
Pang
  • 9,564
  • 146
  • 81
  • 122
3

i am using android studio

<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="com.app.myapp" >

    <item
        android:id="@+id/search"
        android:title="Buscar"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView"
        app:actionViewClass="android.widget.SearchView"/>

</menu>

found 100%

  • Does that mean it's solved? Because I see in your XML the id is "search", but you are looking for "R.id.menu_search". Shouldn't that be "R.id.search"? – r0ber7 Mar 13 '14 at 15:34
  • no, android studio configure all in u android project, no is just the xml in the menu – Erick Guardado Apr 21 '14 at 17:46
0

To reference a layout from a menu xml use the actionLayout attribute:

<menu>
<item android:id="@+id/menu_name"
  android:actionLayout="@layout/your_layout"
  android:showAsAction="always"/>
</menu>
dmon
  • 30,048
  • 8
  • 87
  • 96
  • Any idea how to create list search view in fragment? – Sanjeev Yadav Jul 27 '13 at 21:31
  • Now getting by implementing your changes java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.SearchView at com.sears.syw.FragmentShopsTab.onCreateOptionsMenu(FragmentShopsTab. java:52) at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55) at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559) at – Sanjeev Yadav Jul 28 '13 at 05:04
0

Some times problem is with

setHasOptionsMenu(true);

Move it from oncreate or onviewcreate to on attached method and try it.

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77
0

Check this answer Either you are using wrong namespace or importing wrong class https://stackoverflow.com/a/38702262/5374951

Community
  • 1
  • 1
ketankk
  • 2,578
  • 1
  • 29
  • 27