0

I want to open SearchResultsActivity when user enters a search query but I have been trying to setup search view in my activity from past 1 day without any success. I have seen lot of stackoverflow questions and tutorials. None of the solutions work for me.

I have this in my application tab of AndroidManifest

    <meta-data
        android:name="android.app.default_searchable"
        android:value="<package>.search.SearchResultsActivity" />

This is my SearchResultsActivity declared in manifest

     <activity
        android:name="search.SearchResultsActivity"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
        </intent-filter>

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

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" />

This is the activity where I want this search view

    <activity
        android:name=".goods.GoodsActivity"
        android:screenOrientation="portrait"/>

Here on options create menu of said activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.base_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
    ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));

    return true;
}

I constantly am getting following crash with this code

java.lang.NullPointerException: Attempt to read from field 'android.os.Bundle android.content.pm.PackageItemInfo.metaData' on a null object reference
at android.os.Parcel.readException(Parcel.java:1605)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ISearchManager$Stub$Proxy.getSearchableInfo(ISearchManager.java:177)
at android.app.SearchManager.getSearchableInfo(SearchManager.java:851)
at <package>.BaseActivity.onCreateOptionsMenu(BaseActivity.java:83)
at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
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)
Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69

2 Answers2

0

I had the same issue when upgrading to Android 7. What solved this for me for Xamarin, was to use next line

ComponentName component = new ComponentName(PackageName, Java.Lang.Class.FromType(type).Name);
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(component));

instead of

ComponentName component = new ComponentName(PackageName, nameof(SearchableActivity));
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(component));

Thanks to lahsrah, Xamarin Android how to get Java class name for passing into ComponentName

jamie
  • 819
  • 10
  • 5
0

Also faced same issue in Java, for some strange reason I only faced this on Android 7.1.1 and Android 8.1. It worked fine on 6, 10 and 11. Anyway, for me below fixed it,

Instead of using, ComponentName componentName=new ComponentName(this, .class);

Used below, componentName=new ComponentName(this,Class.forName().getClass());

This solved it on all Android versions starting 6.0 and above.

lazers
  • 21
  • 3