-->

Android search activity not starting

2020-04-12 13:27发布

问题:

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>

回答1:

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



回答2:

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