Android SearchManager 在 Instant App 中为 null
Android SearchManager is null in Instant App
我使用的是一个简单的搜索对话框,它在可安装的应用程序中运行良好,但只有当 运行 代码作为即时应用程序时,我才会收到 NullPointerException:
java.lang.NullPointerException: Attempt to invoke interface method
'android.app.SearchableInfo
android.app.ISearchManager.getSearchableInfo(android.content.ComponentName)'
on a null object reference
所有代码和资源都在基本模块中。
这发生在我的启动器的 onCreateOptionsMenu 中 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;
}
这是 menu.xml 文件(使用 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>
searchable.xml文件如下,以防万一:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >
</searchable>
Activity 在 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>
可安装的应用程序运行良好。知道为什么会发生这种情况吗?
您需要为旧设备的即时应用程序更新 Google Play 服务。
在您的设备上:
转到Settings
> Apps
> Google Play services for Instant Apps
> Uninstall
通知应该会提示您恢复它 > restore it
这应该将其更新到最新版本,该版本应该不会再导致此崩溃,目前为 2.11-release-183080821
。
注意:- 不幸的是,这 不适用于模拟器 目前,Android Studio 将安装-最新版本。
我使用的是一个简单的搜索对话框,它在可安装的应用程序中运行良好,但只有当 运行 代码作为即时应用程序时,我才会收到 NullPointerException:
java.lang.NullPointerException: Attempt to invoke interface method 'android.app.SearchableInfo android.app.ISearchManager.getSearchableInfo(android.content.ComponentName)' on a null object reference
所有代码和资源都在基本模块中。
这发生在我的启动器的 onCreateOptionsMenu 中 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;
}
这是 menu.xml 文件(使用 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>
searchable.xml文件如下,以防万一:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >
</searchable>
Activity 在 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>
可安装的应用程序运行良好。知道为什么会发生这种情况吗?
您需要为旧设备的即时应用程序更新 Google Play 服务。
在您的设备上:
转到
Settings
>Apps
>Google Play services for Instant Apps
>Uninstall
通知应该会提示您恢复它 >
restore it
这应该将其更新到最新版本,该版本应该不会再导致此崩溃,目前为 2.11-release-183080821
。
注意:- 不幸的是,这 不适用于模拟器 目前,Android Studio 将安装-最新版本。