2

I'm trying to implement searching in my android app. There are two activities, the MainActivity where search is launched (via searchview in action bar) and ResultsActivity, where search is handled and a list is inflated with results. The problem is that the getSearchableInfo method that i invoke in onCreateOptionsMenu in the main activity returns a null, like no searchable.xml were attached to the activity. I'v tried many combinations of meta data arrangement in the manifest, also every solution found on the internet and other stackoverflow questions, but nothing seems to work. These are the sources:

AndroidManifest.xml

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light" >

    <activity
        android:name="com.example.webparser.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.searchable"
            android:resource="@xml/searchable" />

    </activity>


    <activity android:name="com.example.ResultsActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>


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

</application>

** MainActivity.java **

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    SearchManager searchManager = (SearchManager)  getSystemService(getApplicationContext().SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    Log.d("Search:", searchManager.getSearchableInfo(getComponentName())
            .toString());
    return true;
}

** ResultsActivity.java **

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        elementi = new ArrayList<String>();
        for(Ricetta ricetta:ricette){
            if( ricetta.getNome().toLowerCase().matches(query) ){
                elementi.add(ricetta.getNome());
            }
        }


        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                elementi);
        lista.setAdapter(adapter);
        Log.i("LOG:", "searching");
    }
}

** searchable.xml **

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

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

Please notice that I inserted a Log.d where the searchview is supposed to be initialized, and where is called getSearchableInfo, so at runtime a nullpointerexception is raised and the app doesn't launch, but if that Log.d is removed then everything but the search works (on pressing enter in the search box nothing happens). I think there is a problem with the manifest, but I can't figure out what it is. Does anyone have any clue? I also tried to launch the main activity in singleTop pointing the searchable meta data tags to the main activity, but the same exception is raised.

3 Answers 3

0

I notice that you are using Menu to try an obtain your search view instead of the findViewById function

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

Should be more like this

SearchView searchView = (SearchView) findViewById(R.id.search);

This would give you the SearchView you are looking for.

0

Faced the same problem. Following helped me:

  • This is my MainActivity

    public boolean onCreateOptionsMenu(Menu menu) {       
      getMenuInflater().inflate(R.menu.menu_list_view_test, menu);
      SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
      MenuItem searchItem = menu.findItem(R.id.search);
      SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
      searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );
    return true;
    

Note: Make sure that you have imported SearchView from Support.v7.widget (import android.support.v7.widget.SearchView;)

  • This is my Menu.xml for MainActivity

    < item android:id="@+id/search" android:title="Search" android:icon="@drawable/ic_action_search" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" />

0

In my case I have similar set up just a little bit different. I have:

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

in my Main Activity. And

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

and

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

should work.

in my ResultActivity. And I create a component name in my MainActivity with:

ComponentName componentName = new ComponentName(context, ResultActivity.class);

Note: be careful with your item layout:

Use: app:actionViewClass="android.support.v7.widget.SearchView" if you have problem with android:actionViewClass="android.support.v7.widget.SearchView"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.