无法获取搜索查看在动作条工作看在、动作、工作

2023-09-12 00:11:15 作者:战与败 ;爱或毁 ;

我是pretty的新的Java和Android开发;我一直在一个应用程序中的巫婆,我想与一个搜索查看的操作栏。我已经看过了谷歌的例子,但我不能得到它的工作。我必须做一些错误的,我即将放弃对应用程序开发:DI已经搜查,但我没有发现任何有用的。也许你们可以帮助我:)

I am pretty new to java and android development; and I have been working on an app in witch I want an action bar with a SearchView. I have looked at the google examples but I can not get it to work. I must be doing something wrong and I'm about to give up on app-development :D I have searched but I have not found anything helpful. Maybe you guys can help me :)

问题:我得到的搜索视图打开,因为它应该,但之后没有任何反应可言,我发现我的 searchManager.getSearchableInfo(getComponentName())的返回null。另外我暗示,我已经给了未在我的搜索框魔女显示使我相信,也许该应用程序无法找到我的searchable.xml?说得够多了:)

The problem: i get the search view to open as it should, but after that nothing happens at all, I've noticed that my searchManager.getSearchableInfo(getComponentName()) returns null. Also my hint that I've given is not displayed in my search box witch leads me to believing that maybe the app can't find my searchable.xml? Enough talk :)

MainActivity.java

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

        return true;
    }
}

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

Androidmanifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.searchapp"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

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

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

    </application>

</manifest>

SearchableActivity.java

package com.example.searchapp;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SearchableActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
    }

    private void doMySearch(String query) {
        Log.d("Event",query);
    }
}

这里是一个链接所指的项目,我会永远感激,如果有人是为了帮助我这个!

And here is a link to the referred project, i would be eternally grateful if someone was to help me with this!

来源$ C ​​$ C

推荐答案

您的问题是在你的Andr​​oidManifest。我几乎完全一样,你这样做,因为这是我得到下面的文档。但目前还不清楚或错误的。

Your problem is in your AndroidManifest. I had it almost exactly as you do, because that is what I get following the documentation. But it is unclear or mistaken.

看着在API演示源我发现了android.app.searchable元数据必须去你的结果的活动,并在主要活动(或您将搜索查看活动),你点到另一个之一,android.app.default_searchable。

Watching at the API Demos source I found that the "android.app.searchable" metadata must go in your "results" activity, and in the main activity (or the activity where you place the SearchView) you point to the other one with "android.app.default_searchable".

您可以看到它在下面的文件,这是清单从我的测试项目:

You can see it in the following file, which is the Manifest from my test project:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <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=".SearchActivity"
        android:label="@string/app_name" >

        <!-- This intent-filter identifies this activity as "searchable" -->

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

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <!-- This metadata entry provides further configuration details for searches -->
        <!-- that are handled by this activity. -->

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

同样重要的是,在searchable.xml的提示和标签的参考,而不是硬codeD字符串。

It is also important that the hint and label in the searchable.xml are references, and not hardcoded strings.

我希望它可以解决你的问题。我花了一整天的数字出来:(

I hope it solves your problem. It took me all day to figure it out :(