1

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

清单.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>

MainActivity.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);
        }
    }
}

可搜索的.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
4

2 回答 2

2

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

<?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-21T17:42:35.250 回答
0

您的 XML 格式不正确。检查 XML 中的错误。有一个额外的关闭>。您似乎没有为您的 XML 元素提供 ID。

于 2013-04-21T17:43:50.833 回答