Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView is null android

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!!!!

like image 697
Rodrigo De Oliveira Murta Avatar asked Jan 06 '15 18:01

Rodrigo De Oliveira Murta


1 Answers

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"
like image 191
shyam Avatar answered Sep 22 '22 06:09

shyam