Android SearchView小部件不起作用

时间:2015-08-05 17:06:40

标签: android search android-intent android-activity

我在操作栏中运行searchview时遇到问题...

问题是,我的应用程序显示了搜索视图,但是当我尝试搜索时 没有任何反应。

这是SearchActivity类:

public class MainActivity extends AppCompatActivity
    implements SearchListFragment.OnCardSelectedListener
{
    private static final String TAG = "MainActivity";
    private static final String idLanguage = "1";

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.body_fragment) != null)
        {
            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null)
            {
                return;
            }
        }
    }

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

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));

        searchView.setIconifiedByDefault(false);            

        return super.onCreateOptionsMenu(menu);
    }
}

这是我的清单文件:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="20" />

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

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

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

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

        </activity>

        <activity
            android:name=".SearchResultsActivity"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity" >

            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

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

        </activity>

    </application>

</manifest>

最后这是我的SearchActivity类:

package eu.mcm.prindl.magiccardmarket;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by konstantin on 8/5/15.
 */

public class SearchResultsActivity extends Activity
{
    private static final String TAG = "SearchResultsActivity";

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result_layout);

        handleIntent(getIntent());
    }

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

    /**
     * Handling intent data
     */
    private void handleIntent (Intent intent)
    {
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Debug.write(TAG,"Search Query: " + query);

        }

    }
}

请帮忙。我已经在Stackoverflow上寻找了各种解决方案,但似乎都没有用(但也许我只是错过了一些东西)。

更新: 这是我的searchable.xml:

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

1 个答案:

答案 0 :(得分:1)

您的清单应该看起来更像以下内容:

(type)value

匹配的XML文件包含类似

的内容
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<application android:name="..." android:icon="@drawable/logo" android:label="@string/app_name" android:theme="@style/...">
    <activity android:name=".MainActivity" android:label="@string/app_name"
        android:launchMode="singleTop" android:theme="@style/AppTheme">
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
        <intent-filter>
             <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        ...
    </activity>
    ...
</application>

另外,请确保您的活动实际设置了搜索视图:

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