2013-04-21 27 views
1

您好我正在嘗試創建一個Android應用程序的搜索界面。我跟着android開發者教程,但我仍然收到錯誤。我不知道我哪裏出了問題,請幫忙!我正在嘗試將搜索小部件集成到我的應用程序的佈局中。下面是我迄今所做的:在Android應用程序搜索界面的問題

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

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 
+0

發佈您的logcat跟蹤..或發佈文本,如果有語法錯誤 – Pragnani 2013-04-21 17:28:36

+0

我已經包含錯誤 – wyounis 2013-04-21 17:39:49

+0

選擇最佳答案將激勵人們回答您的未來問題。 – RyPope 2013-04-21 18:54:35

回答

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> 
+0

謝謝!正在處理同樣的問題。很高興終於降落在這個環節。 – 2015-09-01 10:49:00

0

你的XML格式不正確。檢查XML中的錯誤。有一個額外的收盤>。您尚未向您的XML元素提供ID。