java - SearchView 未启动搜索 Activity ,但搜索栏打开

标签 java android searchview

我在为我的应用程序创建搜索功能时遇到问题。我遵循了各种教程、Android 文档和其他 Stack Overflow 答案,但没有成功。我有每个 Activity 的图标 (ContinentActivity.java),单击时工具栏会打开一个搜索窗口。但是,键入数据不会在 SearchResultsActivity 中注册。它永远不会被创建。这可能是因为我使用了工具栏和菜单吗?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_swell_alert_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

    <activity
        android:name=".SearchResultsActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">

        <!-- to identify this activity as "searchable" -->
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />             
        </intent-filter>
        <meta-data
            android:name="android.appcompat.searchable"
            android:resource="@xml/searchable" />
    </activity>

    <activity
        android:name=".ui.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ui.locations.LocationSelectionActivity"
        android:label="@string/title_activity_location_selection"
        android:noHistory="true"
        android:parentActivityName=".ui.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ui.MainActivity" />
    </activity>
    <activity
        android:name=".ui.locations.ContinentActivity"
        android:theme="@style/NoAnimationTheme"
        android:label="@string/title_activity_continent"
        android:parentActivityName=".ui.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ui.MainActivity" />
    </activity>

    <!-- Children of Continent Activity. Each have search capabilities-->
    <activity
        android:name=".ui.locations.CountryActivity"
        android:theme="@style/NoAnimationTheme"
        android:label="@string/title_activity_country"
        android:parentActivityName=".ui.locations.ContinentActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ui.locations.ContinentActivity" />
    </activity>
        <activity
            android:name=".ui.locations.StateActivity"
            android:theme="@style/NoAnimationTheme"
            android:label="@string/title_activity_state"
            android:parentActivityName=".ui.locations.CountryActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ui.locations.CountryActivity" />
        </activity>
            <activity
                android:name=".ui.locations.SurfSpotActivity"
                android:theme="@style/NoAnimationTheme"
                android:label="@string/title_activity_surf_spot"
                android:parentActivityName=".ui.locations.StateActivity" >
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value=".ui.locations.StateActivity" />
            </activity>
</application>

SearchResultsActivity.java

public class SearchResultsActivity extends AppCompatActivity {
public static final String TAG = "SEARCH_RESULTS_ACTIVITY";
SearchResultsAdapter mSearchResultsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.v(TAG, "onCreate");
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.v(TAG, "onNewIntent");
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    Log.v(TAG, "HANDLE INTENT");
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);
    }
}

private void showResults(String query) {
    // Query your data set and show results
    // ...
    Log.v(TAG, "Searching for " + query + "...");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    Log.v(TAG, "onCreateOptionsMenu");
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_location, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );

    return true;
}

此 Activity 菜单有搜索图标 ContinentActivity.java

public class ContinentActivity extends AppCompatActivity {

private ArrayList<Continent> mContinents;
@Bind(R.id.recyclerView) RecyclerView mRecyclerView;
@Bind(R.id.toolBar) Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_selection);
    ButterKnife.bind(this);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    LocationDataSource dataSource = new LocationDataSource(this);
    dataSource.test();
    mContinents = dataSource.readContinents();

    ContinentAdapter adapter = new ContinentAdapter(this, mContinents);
    mRecyclerView.setAdapter(adapter);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_location, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}
}

menu_location.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:appcompat="http://schemas.android.com/apk/res-auto">

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

</menu>

tool_bar.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    android:titleTextColor="@color/ColorText">

</android.support.v7.widget.Toolbar>

最佳答案

似乎您在 onCreateOptionsMenu() 方法中缺少将 SearchView 与可搜索信息关联的调用?引用自培训链接http://developer.android.com/guide/topics/search/search-dialog.html

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

但是由于您没有对搜索结果使用相同的 Activity,因此您不能简单地使用 getComponentName(),而是使用 new ComponentName(this, SearchResultsActivity.class)

关于java - SearchView 未启动搜索 Activity ,但搜索栏打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201692/

相关文章:

java - Spring bean 范围 : session and globalSession

android - 将图像从资源设置为可绘制对象的区别?

android - 按持续时间过滤 Android StrictMode 违规

java - 尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.clearTextFilter()'

android - 语音实现后的 SearchView 抛出 NullPointerException

java - 日期格式 日/月/年

java - 执行语句有很长的延迟

java - 如果内存不稀缺,您将如何用一种语言实现一种排序,其中包含用于表示和排序集合的库

java - 没有 GCM 的 Android 推送通知可以在后台服务中工作,但推送通知会在预计时间之前显示

android - 在 Android 操作栏中设置 SearchView 的样式