Search code examples
androidandroid-searchmanager

getSearchableInfo returns NULL


After trying ALL of the solutions in stackoverflow about this problem, I decided to ask it here.

I'm trying to implement search bar in my Action bar. I'm using AppCompatActivity and imported android.support.v7.widget.SearchView.

For the record, I'm using PageManager and TabLayout if it has any releation.

I get java.lang.NullPointerException error in the following line:

searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

This is my code:

search bar declration at menu_main.xml:

<item
        android:id="@+id/searchPlace"
        android:title="@string/search_hint"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:showAsAction="always"/>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>

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

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
        <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>

            <meta-data
                android:name="android.app.default_searchable"
                android:value=".searchResultActivity" />

        </activity>
        <activity
            android:name=".Settings"
            android:label="@string/title_activity_settings" >
        </activity>

        <activity
            android:name=".searchResultActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">

            <!-- to identify this activity as "searchable" -->
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

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

Search code in my MainActivity.java onCreateOptionsMenu:

// Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.searchPlace).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

Logcat error:

Process: il.co.test.test, PID: 1642
java.lang.NullPointerException
        at il.co.test.test.MainActivity.onCreateOptionsMenu(MainActivity.java:106)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:267)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
        at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
        at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

UPDATE

I'm not sure, and I don't know how to check it - but I am pretty sure my getActionVIew() returns NULL.

UPDATE 2 - FIXED!

After checking some stackoverflow's about getActionView() null problem, The 2nd answer at getActionView() of my MenuItem return null fixed my problem. I had to use namespace app instaed of android.

Really painful!

Hope someone can find my mistake and help me fix it, since I tried many solutions for 6 hours.

Thanks in advance!


Solution

  • After checking some stackoverflow's about getActionView() null problem, The 2nd answer at getActionView() of my MenuItem return null fixed my problem. I had to use namespace app instaed of android.

    Really painful!