Android 搜索活动未启动

发布于 2024-11-02 19:07:21 字数 3212 浏览 0 评论 0原文

当我按下 Android 手机上的搜索按钮时,我的搜索框不显示。我想要做的是执行 ASyncTask(或后台任务)来获取字符串数组(人名)的 JSON 响应,搜索结果,并使用 IMDB 具有的相同搜索功能将其显示给用户。目前,我正在使用字符串数组来测试我的搜索响应/请求。有什么想法吗?

按下搜索按钮时,我收到以下警告(但我认为这与我的问题无关):

04-21 03:35:23.767: WARN/KeyCharacterMap(12946): Can't open keycharmap file
04-21 03:36:48.978: WARN/KeyCharacterMap(12999): Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.65537.devname='qtouch-touchscreen'
04-21 03:37:21.408: WARN/KeyCharacterMap(2576): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

以下错误已由 @Heiko Rupp 修复:

04-21 03:17:51.994: WARN/SearchableInfo(1293): Invalid searchable metadata for com.redacted/.SearchActivity: Search label must be a resource reference.

SearchActivity.java:

package com.redacted;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class SearchActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_results);
        handleIntent(getIntent());
    }

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

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            showResults(query);
        }
    }

    private void showResults(String q) {
        String[] listItems = { "test", "my", "search", "list" };

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems));
    }
}

searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:searchButtonText="Search" 
  android:label="Redacted" 
  android:hint="Redacted"
  android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>

Manifest Manifest.xml 的一部分(元-data 在标签内,因此它是全局的):

<activity android:launchMode="singleTop" android:name="SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable" 
                android:resource="@xml/searchable"/>
        </activity>
        <meta-data android:name="android.app.default_searchable"
            android:value="SearchActivity" />
    </application>

search_results.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/list"></ListView>
</LinearLayout>

My search box is not displaying when I press the search button on my Android phone. What I want to do is do an ASyncTask (or background task) to get a JSON response of an array of strings (names of people), search through the result, and display it to the user using the same search functionality that IMDB has. Currently, I'm using a string array to test my search response/request. Any ideas?

I get the following warnings when pressing the search button (but I think this is unrelated to my issue):

04-21 03:35:23.767: WARN/KeyCharacterMap(12946): Can't open keycharmap file
04-21 03:36:48.978: WARN/KeyCharacterMap(12999): Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.65537.devname='qtouch-touchscreen'
04-21 03:37:21.408: WARN/KeyCharacterMap(2576): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

The error below has been fixed courtesy of @Heiko Rupp:

04-21 03:17:51.994: WARN/SearchableInfo(1293): Invalid searchable metadata for com.redacted/.SearchActivity: Search label must be a resource reference.

SearchActivity.java:

package com.redacted;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class SearchActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_results);
        handleIntent(getIntent());
    }

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

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            showResults(query);
        }
    }

    private void showResults(String q) {
        String[] listItems = { "test", "my", "search", "list" };

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems));
    }
}

searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:searchButtonText="Search" 
  android:label="Redacted" 
  android:hint="Redacted"
  android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>

Part of Manifest Manifest.xml (meta-data is inside tag so it's global):

<activity android:launchMode="singleTop" android:name="SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable" 
                android:resource="@xml/searchable"/>
        </activity>
        <meta-data android:name="android.app.default_searchable"
            android:value="SearchActivity" />
    </application>

search_results.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/list"></ListView>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南月 2024-11-09 19:07:21

我遇到了同样的问题。 android:label 的值必须是字符串资源。硬编码字符串不会起作用。

I got in to the same problem.The value for android:label must be a string resource.Hardcoding strings won't wok.

月牙弯弯 2024-11-09 19:07:21

错误消息显示“搜索标签必须是资源引用”。

所以

<searchable
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:label="Redacted" 

是错误的,应该阅读

<searchable
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:label="@string/Redacted" 

然后在 strings.xml 中有一个“Redacted”条目

您的清单显示

android:value="SearchActivity" />

不应该是这样吗

android:value=".SearchActivity" />

The error message says "search label must be a resource reference."

So

<searchable
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:label="Redacted" 

is wrong and should rather read

<searchable
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:label="@string/Redacted" 

And then have an entry for 'Redacted' in strings.xml

Your Manifest shows

android:value="SearchActivity" />

Shouldn't that be

android:value=".SearchActivity" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文