Android 搜索活动未启动
当我按下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题。 android:label 的值必须是字符串资源。硬编码字符串不会起作用。
I got in to the same problem.The value for android:label must be a string resource.Hardcoding strings won't wok.
错误消息显示“搜索标签必须是资源引用”。
所以
是错误的,应该阅读
然后在 strings.xml 中有一个“Redacted”条目
您的清单显示
不应该是这样吗
The error message says "search label must be a resource reference."
So
is wrong and should rather read
And then have an entry for 'Redacted' in strings.xml
Your Manifest shows
Shouldn't that be