TheUnreal
TheUnreal

Reputation: 24492

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!

Upvotes: 0

Views: 1041

Answers (2)

kolombo
kolombo

Reputation: 1091

Search menu item

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

Activity code

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.your_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.searchPlace).getActionView();
    searchView.setOnQueryTextListener(queryTextListener);
    return true;
}

private OnQueryTextListener queryTextListener = new OnQueryTextListener()
{
    @Override
    public boolean onQueryTextSubmit(String query) {
        //if you need this
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        //your code
        return true;
    }
};

In this this case you dont need searchable configuration, dont need strokes in manifest, dont need SearchManager and other stuff. Just declare search item in the menu and thats it.

Upvotes: 1

TheUnreal
TheUnreal

Reputation: 24492

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!

Upvotes: 0

Related Questions