我的应用程序中的SearchView似乎不起作用

Lor*_*ers 2 android searchview

我已经实现了以下代码,但我的SearchActivity :: OnCreate没有被调用.有人可以告诉我我错过了什么吗?

AndroidManifest.xml中:

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

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity"/>
        <activity
            android:name=".Search_testActivity"
            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=".SearchActivity" android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </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>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

/res/xml/searchable.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/description" >
</searchable>
Run Code Online (Sandbox Code Playgroud)

SearchActivity.java:

public class SearchActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            int q;
            q = 10;
        }
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
    }

}
Run Code Online (Sandbox Code Playgroud)

bak*_*kua 15

Android指南明确指出:

// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Run Code Online (Sandbox Code Playgroud)

在你的情况下,这是不正确的.因为您不想在MainActivity中搜索,而是在SearchableActivity中搜索.这意味着您应该使用此ComponentName而不是getComponentName()方法:

ComponentName cn = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
Run Code Online (Sandbox Code Playgroud)