2010-10-28 42 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花费我们的时间,所以要小心!