Android SearchView 无法转到 SearchResultsActivity

Android SearchView can't go to the SearchResultsActivity

我正在按照 google android 参考 https://developer.android.com/training/search/setup.html 来实现我的应用程序的 SearchView,但是当输入完成并单击键盘的右下角时,它并没有转到没有错误信息的 SearchResultsActivity。

我的 SearchView 在 NotesActivity 中,当用户输入关键字完成时,它将转到 SearchResultsActivity,其中有一个 recyclerView。 这是我的 manifest.xml :

    <activity
        android:name=".notes.NotesActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"/>

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity
        android:name=".search.SearchResultsActivity"
        android:label="@string/title_activity_search_results"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
        </intent-filter>
    </activity>

这是 NotesActivity.java

@Override
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);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    return true;
}

这是 SearchResultsActivity 的主要代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_results);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        mKeyword = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
        if (!TextUtils.isEmpty(mKeyword)) {
            mPresenter.loadNotes(mKeyword);
        } else {
            Snackbar.make(mRecyclerView, R.string.search_empty_keyword, Snackbar.LENGTH_SHORT)
                    .show();
        }
    }
}

menu/main.xml:

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_search_white_24dp"
    android:orderInCategory="100"
    android:title="@string/action_search"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="always"/>

xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:hint="@string/search_hint"
        android:label="@string/app_name"/>

请指导。

您在 SearchResultsActivity

中丢失
<category android:name="android.intent.category.DEFAULT" />

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

NotesActivity 中,链接是通过

完成的
  <meta-data
        android:name="android.app.default_searchable"
        android:value=".SearchResultsActivity" />

searchable 与显示搜索对话框的 activity 有关,而 default_searchable定义使用哪一个来提供搜索。

它的正确文档在这里https://developer.android.com/guide/topics/search/search-dialog.html