0

I'm using a simple search dialog that works fine in the installable app, but I'm getting a NullPointerException only when running the code as an Instant App:

java.lang.NullPointerException: Attempt to invoke interface method 'android.app.SearchableInfo android.app.ISearchManager.getSearchableInfo(android.content.ComponentName)' on a null object reference

All code and resources are inside the base module.

This happens inside onCreateOptionsMenu in my launcher activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_entity, 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;
}

This is the menu.xml file (using appcompat):

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:android="http://schemas.android.com/apk/res/android">

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

The searchable.xml file is as follows, just in case:

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

Activity in Manifest.xml:

<activity android:name=".EntityActivity">
    <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>

The installable app works fine. Any ideas why this could be happening?

rednuht
  • 1,724
  • 16
  • 27
  • which manifest defines searchable? – dazza5000 Jan 16 '18 at 20:39
  • @dazza5000 It's the base module manifest. Thanks for checking the question. – rednuht Jan 16 '18 at 20:42
  • possible duplicate of https://stackoverflow.com/questions/27049294/searchview-doesnt-work-since-app-compat – ReyAnthonyRenacia Jan 17 '18 at 05:57
  • @noogui Similar, but not really. That question is about a NPE on the SearchView itself, not on the SearchManager, as is my case. Also, I'm only having problems with Instant Apps. The whole code works fine if it's run from a regular installable APK. – rednuht Jan 17 '18 at 13:20
  • Can you file a bug to Google, then link to it back in here? It would be helpful for all, thanks! https://issuetracker.google.com/issues/new?component=316045&template=1018787 – TWL Jan 19 '18 at 22:28
  • Here's the bug report: https://issuetracker.google.com/issues/72685123 – rednuht Jan 30 '18 at 18:04

1 Answers1

1

You need to update your Google Play services for Instant Apps for pre-Odevices.

On your device:

  1. go to Settings > Apps > Google Play services for Instant Apps > Uninstall

  2. A notification should prompt you to you to restore it > restore it

This should update it the latest version which should no longer cause this crash, currently at 2.11-release-183080821.

Note:- Unfortunately, this does not apply to emulators as of now, Android Studio will install the out-of-date version to it.

Prags
  • 2,457
  • 2
  • 21
  • 38