【问题标题】:SearchView doesn't start search activitySearchView 不启动搜索活动
【发布时间】:2015-05-04 08:38:40
【问题描述】:

我正在尝试使用搜索栏中的搜索视图添加搜索活动。我已经看到了一些这样的问题,但我已经尝试了所有答案,但对我没有任何帮助。搜索显示正确,但是当我输入搜索短语并单击搜索后,没有任何反应。 我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.y2apps.quoteformessenger.app"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="android.app.default_searchable" android:resource="@xml/searchable" android:value=".SearchResultsActivity"/>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <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=".SearchResultActivity"
        android:theme="@style/AppTheme"
        android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
                <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
    </activity>
</application>

可搜索的.xml

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

styles.xml 文件

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

主要活动

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayShowHomeEnabled(false);
    }

    @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.search).getActionView();
        if (searchView != null) {
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
        } else {
            Toast.makeText(this, "can't find search bar", Toast.LENGTH_LONG).show();
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

和搜索活动

public class SearchResultActivity extends Activity {
    // ===========================================================
    // Public Constants
    // ===========================================================

    // ===========================================================
    // Private constants and Fields 
    // ===========================================================

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Public methods
    // ===========================================================
    @Override
    public void onCreate(Bundle savedInstanceState) {
        handleIntent(getIntent());
    }
    // ===========================================================
    // Private methods
    // ===========================================================
    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Toast.makeText(this, "YARON YOREH 2", Toast.LENGTH_LONG).show();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(this, "YARON YOREH asked for " + query, Toast.LENGTH_LONG).show();
            //use the query to search your data somehow
        }
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================

}

如果有助于诊断,搜索提示也不会出现。 谢谢你的帮助

【问题讨论】:

    标签: android xml searchview


    【解决方案1】:

    您的清单中有错字:

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

    我认为应该是:

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

    但是,您应该不需要,因为您已经为您的应用程序定义了全局可搜索的默认值

    【讨论】:

      【解决方案2】:

      根据您的代码,我理解的是您的 MainActivity 是搜索活动,您希望在新活动中显示结果。为此,您必须将 MainActivty 设为可搜索。如下所示更改您的清单。 执行搜索的主要活动

           <activity
                  android:name=".MainActivity"
                  android:label="@string/app_name" >
                  <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>
      

      显示结果的活动

              <activity android:name=".SearchResultActivity" ... >
      
              <meta-data android:name="android.app.default_searchable"
                         android:value=".MainActivity" />
          </activity>
      

      【讨论】:

      • 谢谢 Gautam 的回答,但我的主要活动是有搜索栏的主要活动,搜索活动是我的搜索活动。我的问题是如下所述的错字。但是感谢您花时间阅读它
      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多