4

I am just a beginner and i searched this question here found 4-5 question but that did not helped me.I just trying searchView in Actionbar, My activity is AppCompatActivity my code is below.I am using toolbar in this activity. and using this library too. OncreatOptionMenu:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    SearchManager SManager =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
    searchViewAction.setIconifiedByDefault(false);

    return super.onCreateOptionsMenu(menu);
}

Xml:

<item android:id="@+id/action_search"
    android:title="action_search"
    MainActivity:showAsAction="ifRoom"
    MainActivity:actionViewClass="android.support.v7.widget.SearchView" />

and logcat:

java.lang.NullPointerException
        at algonation.com.spm.MainActivity.onCreateOptionsMenu(MainActivity.java:183)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2571)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImplBase.java:251)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:449)
        at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:66)
        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:5292)
        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:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

If you need whole code of MainActivity please let me know.. Thank you so much.

Akshay chauhan
  • 145
  • 4
  • 12
  • 1
    This is right answer for your question [http://stackoverflow.com/questions/27049294/searchview-doesnt-work-since-app-compat][1] [1]: http://stackoverflow.com/questions/27049294/searchview-doesnt-work-since-app-compat – Hien Nguyen Oct 04 '15 at 18:53

3 Answers3

5

You are getting NullPointerException because below line is returning null.

SManager.getSearchableInfo(getComponentName()

You need to

1 - Declare the activity(which needs to perform search) to accept the ACTION_SEARCH intent, in an <intent-filter> element.

2 - Specify the searchable configuration to use, in a <meta-data> element.

Assuming you are performing search in your MainActivity (which is AppCompatActivity as you want) see the below example:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_main);
}


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

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchViewAction = (SearchView) MenuItemCompat
            .getActionView(searchMenuItem);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchableInfo = searchManager 
            .getSearchableInfo(getComponentName());
    searchViewAction.setSearchableInfo(searchableInfo);
    searchViewAction.setIconifiedByDefault(false);

    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

AndroidManifest.xml

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>

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

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

    </activity>

</application>

menu/activity_main.xml

<item
    android:id="@+id/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom"
    android:title="@string/action_search"/>

res/xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

Hope it will solve your problem & for reference see here.

Amrit Pal Singh
  • 7,116
  • 7
  • 40
  • 50
  • its working but the search interface is always open. unlike.. first we click than it opens for input – Devendra Singh Jun 09 '15 at 07:40
  • 1
    If you want to open search interface after click then replace searchViewAction.setIconifiedByDefault(false); with true in above code present inside MainActivity's onCreateOptionsMenu(). – Amrit Pal Singh Jun 09 '15 at 09:17
  • would you please have a look on it [question](http://stackoverflow.com/questions/30707277/background-color-or-images-shuffling-on-scrolling-in-recyclerview). I do not want to change my adapter for now. i have same adapter as in this question. – Devendra Singh Jun 09 '15 at 09:27
2

This thread is half old, but the responses don't worked for me. What worked for me was change the "app" for "appcompat".

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
<item
    android:id="@+id/action_search"
    android:title="@string/search_title"
    appcompat:actionViewClass="android.support.v7.widget.SearchView"
    appcompat:showAsAction="always"/>
</menu>

Sorry my english.

1

Actually, just make sure you are importing "android.support.v7.widget.SearchView" and NOT "android.widget.SearchView"

import android.support.v7.widget.SearchView
AndreMoreira
  • 31
  • 1
  • 5