9

Hello I am trying to implement a SearchView just like we have in Gmail .

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        getMenuInflater().inflate(R.menu.main, menu);

        searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));


        restoreActionBar();
        return true;
    }

    return super.onCreateOptionsMenu(menu);
}

searchView object is null

Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dgp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".GameSearchActivity" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GameInfoActivity"
            android:label="@string/title_activity_game_info" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".GameSearchActivity"
            android:label="@string/title_activity_game_search" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:value="@xml/searchable" />
        </activity>
    </application>

</manifest>

Searchable.xml

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

menu layout:

<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.example.dgp.MainActivity" >

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="collapseActionView|always"
    android:title="@string/action_search"
    android:actionViewClass="android.support.v7.widget.SearchView"/>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"/></menu>

Can anyone help to understand why is this object getting null? Thank you!!!!

2
  • 7
    shouldn't it be app:actionViewClass? Are you extending ActionBarActivity?
    – Blackbelt
    Commented Jan 6, 2015 at 18:43
  • YEAHH great catch Blackbelt!!! that fixed :D Commented Jan 6, 2015 at 18:55

3 Answers 3

29

In Menu Layout :

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="collapseActionView|always"
    android:title="@string/action_search"
    android:actionViewClass="android.support.v7.widget.SearchView"/>

use this :

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="collapseActionView|always"
    android:title="@string/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

So you need to use app instead of android in the menu item tag :

    app:actionViewClass="android.support.v7.widget.SearchView"
4
  • @StarWind, It was the solution for the question asked above. Please provide the details of your problem.
    – shyam
    Commented Aug 18, 2016 at 15:21
  • I fixed it. My problem was I was trying to access it outside of onCreateOptionsMenu, specifically in oncreate. I wanted to take the result and add it to the search edit text. (Solution is don't and look at the API for searchview more) That said the java portion of code should also be posted so we know what you are doing and for completion of an answer. My apologies for not explaining in full the issue. Commented Aug 18, 2016 at 15:42
  • @StarWind, Everything except that one line is the same as described in the question. So there was no need to copy and paste the whole code that is already in the question.
    – shyam
    Commented Aug 21, 2016 at 3:19
  • Okay, that would have been good informations :-) You get my point. Anyways we are off topic -- how to write good answers. Commented Aug 21, 2016 at 5:26
0

To support Search view you need to use

app:actionViewClass="android.support.v7.widget.SearchView" ----in the menu xml file

And will have to import

android.support.v7.widget.SearchView in the java file

please look carefully imported class have to be android.support.v7.widget.SearchView . when this two parts are not same then null pointer exception may happen.

-1

try

MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = MenuItemCompat.getActionView(searchItem);
2
  • I used: MenuItem searchItem = menu.findItem(R.id.action_search); searchView = (SearchView)MenuItemCompat.getActionView(searchItem); But same error: 01-06 16:51:27.063: E/AndroidRuntime(27106): FATAL EXCEPTION: main 01-06 16:51:27.063: E/AndroidRuntime(27106): Process: com.dgp, PID: 27106 01-06 16:51:27.063: E/AndroidRuntime(27106): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference Thank you Commented Jan 6, 2015 at 18:52
  • have you checked if the menuitem is null? Blackbelts comment is also correct, it should be app:actionViewClass not android:actionViewClass
    – Shooky
    Commented Jan 6, 2015 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.