Search code examples
androidandroid-search

Getting Run time error while setting up search interface for my app


I am designing a search feature for one of my screens in my app. I followed exactly according to android official documentation for setting up search interface

When I run my app I always get runtime error stating that: `AndroidRuntime: FATAL EXCEPTION: main

Process: com.honey.crickapp_project, PID: 31509

java.lang.NullPointerException: Attempt to invoke virtual method 
'void android.support.v7.widget.SearchView.setSearchableInfo
(android.app.SearchableInfo)' on a null object reference at 
com.honey.crickapp_project.Clubs_List.onCreateOptionsMenu(Clubs_List.java:41

Here is my Clubs_List.java file

public class Clubs_List extends AppCompatActivity {
  Toolbar toolbarC;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cricket_clubs);
    toolbarC = (Toolbar) findViewById(R.id.toolbarC);
    setSupportActionBar(toolbarC);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu,menu);
    SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) 
    menu.findItem(search).getActionView(); // this is where it throws null 




searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName())
);
       return true;
    }
}

Here is my menu.xml file

<?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="search"
    android:icon="@drawable/ic_menu_search"
    app:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView"

     />
</menu>

Here is my searchable.xml file

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />

And here is the part of my manifest file

   </activity>

     <activity android:name=".SearchResultsActivity"  >

      <intent-filter>
         <action android:name="android.intent.action.SEARCH" />
      </intent-filter>

   </activity>

Solution

  • Change this android:actionViewClass="android.widget.SearchView"

    to

    app:actionViewClass="android.widget.SearchView"