0

Activity

with #import android.support.v7.widget.SearchView;

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu, menu);

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

        return true;
    }

search_menu xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:yourapp="http://schemas.android.com/tools">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:title="search"
        app:showAsAction="collapseActionView|always"
        yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

when i use support search view it throws an null pointer

java.lang.NullPointerException
        at .MainActivity.onCreateOptionsMenu(MainActivity.java:42)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2585)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:378)
        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
        at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549)
        at android.support.v7.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589)
        at android.support.v7.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869)
        at android.support.v7.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:230)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5333)
        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:828)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
        at dalvik.system.NativeStart.main(Native Method)

but when i change the xml to search widget instead of support widget as follows and change the import on activity

#import android.widget.SearchView;

all works fine

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/tools">
    <item android:id="@+id/search"
        android:title="search"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|always"
        app:actionViewClass="android.widget.SearchView" />

null poitner shows on this line if it helps

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

i have checked the available suggested solutions ..but none of them works..some soulutions are deprecated now

RAINA
  • 802
  • 11
  • 22
  • Possible duplicate of [SearchView getActionView returning null](https://stackoverflow.com/questions/18407171/searchview-getactionview-returning-null) – varunkr Sep 23 '18 at 01:58
  • @varunkr ..thanks man..for helping out.. – RAINA Sep 23 '18 at 02:06
  • @varunkr https://stackoverflow.com/questions/18407171/searchview-getactionview-returning-null this does not fix my issue..i have alredy done all in the above code – RAINA Sep 23 '18 at 02:35
  • i think most suitable answer is here https://stackoverflow.com/questions/18832890/android-nullpointerexception-on-searchview-in-action-bar/18942838#18942838 and this clears the logic behind the exception..but the fix is deprecated now – RAINA Sep 23 '18 at 02:38

0 Answers0