Android SearchView无法访问SearchResultsActivity

时间:2016-12-23 07:18:31

标签: android searchview

我正在关注谷歌Android参考https://developer.android.com/training/search/setup.html来实现我的应用程序的SearchView,但是当条目完成并点击键盘的右下角时,它不会转到SearchResultsActivity没有错误信息。

我的SearchView在NotesActivity中,当用户输入关键字complete时,它将转到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();
        }
    }
}

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

请指导。

1 个答案:

答案 0 :(得分:1)

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

可搜索与显示搜索对话框的活动和 default_searchable 相关,用于定义用于传递搜索的活动。

及其正确的文档在https://developer.android.com/guide/topics/search/search-dialog.html