0

I was looking for a way to add search functionality to my app (specifically on the app bar).

I found a documentation on Andoid devoloper guide

I followed every single step carefully but app crashes.

  1. I Created a Searchable Configuration

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> 
  1. I created a new Searchable Activity called SearchActivity

  2. I declared SearchActivity as Searchable Activty in manifest

Manifest.xml

<application ... >
    <activity android:name=".SearchActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>
    ...
</application>
  1. I Added these in the OncreateOptionsMenu in MainActivity

    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);
    
    // Get the searchview and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    
    return true; }
    
  2. Here is menu/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_search"
        android:title="@string/action_search"
        android:orderInCategory="100"
        android:icon="@drawable/ic_search_white"
        app:showAsAction="ifRoom" />
    

There are 2 Activitues.

  • The MainActivty (which has the Appbar with the search icon as shown in menu_main.xml file)
  • SearchActivity

When the user clicks on the search icon in MainActivity, I want it to go to the SearchActivity and display the search widget there. How do I do this.

What should I add to SearchActivity.java??

Here is the log message i got when the app crashed -

    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.android.fotos, PID: 2613
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
                      at com.example.android.fotos.MainActivity.onCreateOptionsMenu(MainActivity.java:41)
                      at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:358)
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:270)
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
                      at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                      at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Ibrahim
  • 99
  • 12

1 Answers1

0

Try to add

<meta-data
     android:name="android.app.searchable"
     android:value=".SearchActivity" />

in your Manifest.xml inside the application section:

<application ... >
....
<meta-data
     android:name="android.app.searchable"
     android:value=".SearchActivity" />
....
<activity android:name=".SearchActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
</activity>
...

This tells your application how to solve "android.app.searchable"

Look at this and you will find:

If you want every activity in your application to provide the search dialog, insert the above element as a child of the element, instead of each . This way, every activity inherits the value, provides the search dialog, and delivers searches to the same searchable activity. (If you have multiple searchable activities, you can override the default searchable activity by placing a different declaration inside individual activities.) With the search dialog now enabled for your activities, your application is ready to perform searches.

FMiscia
  • 223
  • 1
  • 10
  • Actually, I don't want all activities to provide a search dialog just the SearchActivity. When the user clicks on the search icon in MainActivity, it should go to SearchActivity and the search field should be shown there at the appbar. – Ibrahim Oct 29 '16 at 10:50
  • So try to put it in your main activity definition in the manifest – FMiscia Oct 29 '16 at 10:53
  • I tried it but app still crashes. From the log, the problem is from the MainActivity.java in the OncreateOptionsMenu..Am I not supposed to add anything to the SearchActivity.java which will have the search field. The MainActivity only has the search icon which when clicked goes to the SearchActivity which then displays the search field in the appbar. – Ibrahim Oct 29 '16 at 11:15
  • Since your null pointer expeption is in the`searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));` have tried to read this [post](http://stackoverflow.com/questions/11276043/how-to-add-a-searchwidget-to-the-actionbar) – FMiscia Oct 30 '16 at 12:54