android - Android应用程序中搜索界面的问题

标签 android search

您好,我正在尝试为 Android 应用程序创建搜索界面。我已经按照 android 开发人员教程进行操作,但我仍然遇到错误。我不知道我哪里出错了,请帮忙!我正在尝试将搜索小部件集成到我的应用程序布局中。以下是我到目前为止所做的:

list .xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
           <activity
            android:name="com.example.amazonsearch.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
           <!-- this is the searchable activity; it performs searches -->
    <activity android:name=".SearchableActivity" android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>

    <!-- this activity enables the search dialog to initiate searches
         in the SearchableActivity -->
    <activity android:name=".OtherActivity" >
        <!-- enable the search dialog to send searches to SearchableActivity -->
        <meta-data android:name="android.app.default_searchable"
                   android:value=".SearchableActivity" />
    </activity>
    </application>

</manifest>

主 Activity .java

package com.example.amazonsearch;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SearchView;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, 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();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        return true;
    }



}

SearchableActivity.java

package com.example.amazonsearch;

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.searchresults);

        final Intent queryIntent = getIntent();
        final String queryAction = queryIntent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)) {
            String searchKeywords = queryIntent.getStringExtra(SearchManager.QUERY);
            Log.i("YOU ENTERED", searchKeywords);
        }
    }
}

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android" >
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

这些是以下错误:

[2013-04-21 16:03:36 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Parser exception for E:\Workspace\AmazonSearch\AndroidManifest.xml: Element type "activity" must be followed by either attribute specifications, ">" or "/>".
[2013-04-21 16:03:36 - AmazonSearch] Error in an XML file: aborting build.


Description Resource    Path    Location    Type
menu_search cannot be resolved or is not a field    MainActivity.java   /AmazonSearch/src/com/example/amazonsearch  line 28 Java Problem

Description Resource    Path    Location    Type
options_menu cannot be resolved or is not a field   MainActivity.java   /AmazonSearch/src/com/example/amazonsearch  line 24 Java Problem

Description Resource    Path    Location    Type
searchresults cannot be resolved or is not a field  SearchableActivity.java /AmazonSearch/src/com/example/amazonsearch  line 15 Java Problem

最佳答案

您的可搜索项有两个右括号。试试这个

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

关于android - Android应用程序中搜索界面的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134043/

相关文章:

android - pymtp 在 python 3 上工作时出错

android - 在android中设置选项卡的内容

ios - UITextView如何实现搜索功能?

algorithm - 查找所有出现的子字符串的最快方法是什么?

search - 为什么在我的 Sphinx 中无法搜索到该短语?

android - RecyclerView SnapHelper 无法显示第一个/最后一个项目

android - 实现 Android 通知应用程序的最省电方式

swift - 计算一个特殊字符(非间距标记)在 Swift 中的字符串中出现的次数

ios - 不区分大小写的电子邮件搜索数组

android:编程调用软键盘的问题