cfrz
cfrz

Reputation: 325

setSearchableInfo(searchManager.getSearchableInfo) null pointer exception

I have checked and double checked my manifest according to the docs and other questions. I cannot understand why setSearchableInfo returns NullPointerException

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

    // current activity is not searchable activity
    ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));

    return true;
}

AndroidManifest.xml

<activity
        android:name=".MainActivity"
        android:label="@string/title_main_activity">
        <intent-filter>
            <action android:name="com.csgiusa.treatmentmanager.csgitreatmentmanager.RoomEditDetailed" />
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.csgiusa.treatmentmanager.csgitreatmentmanager.MainActivity" />
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
    </activity>

    <activity
        android:name=".SearchResultsActivity"
        android:label="@string/title_activity_search_results">

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

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

    </activity>

Searchable.xml

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

menu_main.xml

<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=".MainActivity">
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/search"
    android:title="@string/search"
    android:icon="@drawable/ic_search"
    app:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

Upvotes: 2

Views: 1660

Answers (1)

BladeCoder
BladeCoder

Reputation: 12929

Are you using AppCompat and AppCompatActivity ? If yes, you should use the AppCompat version of SearchView (the one in the support.v7 package) and define it in your menu with app:actionViewClass instead of android:actionViewClass.

Then you retrieve it using:

SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));

Upvotes: 6

Related Questions