2010-10-28 44 views
0

我試圖在我的應用程序中實現搜索。如何聲明默認的可搜索活動

我的應用程序包含4個活動,我只想在他們中的3個上添加搜索對話框,而其中只有一個(ProductsActivity)將成爲默認上下文。

不幸的是,雖然我激活搜索我不斷收到以下錯誤: 「Key android.app.default_searchable期望的字符串,但值是一個java.lang.Integer。默認值返回。

<activity android:label="@string/app_name" class=".AppEntry" android:name=".AppEntry"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
<activity android:name=".category.CategoriesListActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
     <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> 
</activity> 
<activity android:name=".product.ProductsActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
     <meta-data android:name="android.app.default_searchable" android:resource="@xml/searchable"/> 
    </activity> 

任何想法爲什麼?

感謝

回答

2

它不應該是

<meta-data android:name="android.app.default_searchable" 
    android:value=".product.ProductsActivity"/> 

,而不是有再次通過@xml參考。

+0

我不認爲,因爲沒有@xml引用系統默認搜索將是系統上下文 – chen 2010-10-28 19:57:37

+1

你有沒有嘗試或你猜測?所有示例,即http://developer.android.com/guide/topics/search/search-dialog.html或http://developer.android.com/resources/samples/Wiktionary/AndroidManifest.html使用我提到的語法,使用android:value而不是android:資源傳遞。我只在這裏談論android.app.default_searchable,而不是android.app.searchable。 – 2010-10-28 20:20:01

+0

好吧,我發現有什麼問題 - 我應該在CategoriesListActivity活動中聲明默認搜索是ProductsActivity – chen 2010-10-28 20:28:07

3

對於默認的可搜索活動,您必須將元數據標記置於應用程序標記之下。

<application ... > 
<meta-data android:name="android.app.default_searchable" 
    android:value=".DefaultSearchActivity"/> 

<activity android:name=".ProductActivity" > 
    ... 
    <meta-data android:name="android.app.default_searchable" 
     android:value=".SearchActivityForProducts"/> 
</activity> 
... 

在該示例中的默認應用程序搜索將在DefaultSearchActivity來完成,而在ProductActivity搜索將是SearchActivityForProducts。希望它能幫助別人。

+0

,但它是不夠的與 2016-08-19 16:31:33

0

一兩件事這裏真正重要的是正確地命名活動爲Android指南解釋了http://developer.android.com/guide/topics/manifest/activity-element.html#nm

android:name The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the element. Once you publish your application, you should not change this name (unless you've set android:exported="false").

There is no default. The name must be specified.

如果你不把DOT你的活動名稱,搜索行動將只對活動工作你聲明爲「default_searchable」。這個小DOT花費我們的時間,所以要小心!