可搜索的配置为SearchView返回Null

Tan*_*yak 4 android searchable searchview oncreateoptionsmenu

我创建了一个搜索界面,但我似乎无法给连接SearchViewActionBar我搜索的配置文件.当我尝试在我的onCreateOptionsMenu方法中检索它时SearchManager.getSearchableInfo(getComponentName()返回null.有人可以帮我解决这个问题吗?

Settings.onCreateOptionsMenu(菜单)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);

    SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView sv = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    SearchableInfo info = sm.getSearchableInfo(getComponentName()); //This variable is returned as null                                                      
    sv.setSearchableInfo(info);
    sv.setIconifiedByDefault(false);
    sv.setSubmitButtonEnabled(true);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中的设置活动

<activity
        android:name=".Settings"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <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>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中的可搜索活动

 <activity
        android:name=".ImageSearch"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

<?xml version="1.0" encoding="utf-8"?>在菜单xml文件中有标题,我尝试清理项目.他们似乎都没有做到这一点.

Tan*_*yak 9

好吧,那算了.经过一番研究后,我发现用于创建搜索界面的android文档并不完整:有些东西被遗漏了

1)在可搜索的配置中不应对字符串进行硬编码

2)我应该<activity>在搜索中使用此代码:

 <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/your search configuration file's name" />
Run Code Online (Sandbox Code Playgroud)

3)我也应该<activity>在我想要SearchView显示的地方有这个代码ActionBar

 <meta-data
            android:name="android.app.default_searchable"
            android:value=".ActivityWhichCarriesOutTheSearch" />
Run Code Online (Sandbox Code Playgroud)

为了完成:这是我的清单文件:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchActivity" />
    </activity>
    <activity android:name=".SearchActivity"
            android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable"/>
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,它仍然不适合我.我的可搜索活动根本不会被解雇! (2认同)