我完全卡住,花了几个小时阅读这个可能的解决方案。根本不能让我的搜索活动启动。
I am completely stuck and have spent hours reading possible solutions to this. Simply can't get my search activity to start.
我有一个主要活动,从中我希望搜索按钮,开始搜索活动:
I have a main activity, from which I want the search button to start a search activity:
文件 MainApp.java
public class MainApp extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainapplayout); } public boolean onCreateOptionsMenu(Menu m){ getMenuInflater().inflate(R.menu.mainappmenu,m); return true; } public boolean onOptionsItemSelected(MenuItem m) { if(m.getItemId()==R.id.menu_search){ return onSearchRequested(); // THIS IS CALLED, AND RETURNS TRUE }else return false; } }无论是pressing的UI按钮,也没有硬件搜索键,调用搜索。 onSearchRequested()绝对是调用,并返回真正。
文件的AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?> <manifest package="org.homphysiology.lists" android:versionCode="1" android:versionName="1.0" xmlns:android="schemas.android/apk/res/android"> <uses-sdk android:minSdkVersion="8" /> <application android:name="org.homphysiology.lists.MainApp" ****REMOVED THIS LINE NOW, thanks arju, still not working tho android:icon="@drawable/ic_launcher"> <activity android:name="org.homphysiology.lists.MainApp"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.default_searchable" android:value="org.homphysiology.lists.SearchActivity" /> </activity> <activity android:name="org.homphysiology.lists.SearchActivity"> <intent-filter> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> ****REMOVED THIS LINE NOW, thanks tushar, still not working tho </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> </application> </manifest>和我有以下内容:
文件 RES / XML / searchable.xml :
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="schamas.android/apk/res/android" android:label="@string/search_label" android:hint="@string/search_hint" />这些字符串是在 /res/values/strings.xml 。
搜索活动本身很简单,但从未创建 - 我在构造函数中断点:
The search activity itself is simple, but is never created - I have a breakpoint in the constructor:
文件 SearchActivity.java :
public class SearchActivity extends ListActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setListAdapter(...) } }任何人都可以发现任何问题吗?[在Eclipse编译朱诺4.2.1 SDK 21.0.1,在HTC Desire Z的测试]谢谢...
Can anyone spot any problem here? [compiling in eclipse Juno 4.2.1, SDK 21.0.1, testing on HTC desire Z] Thanks...
((SearchManager)getSystemService(Context.SEARCH_SERVICE)) .getSearchableInfo(getComponentName())收益空。也许这只是敲敲打打家,我的 SearchActivity 尚未正确注册......事实
returns null. Maybe this is just hammering home the fact that my SearchActivity hasn't been registered properly...
修改@ arju的建议:我可以通过使用手动启动搜索活动
Modifying @arju's suggestion: I can start the Search activity "manually" by using
boolean onOptionsItemSelected(MenuItem m){ startActivity(new Intent(this,SearchActivity.class)); return true; },但当然,这将不会与硬件按钮的工作,而且,它不显示系统搜索对话框
but of course, this won't work with the hardware button, and moreover, it doesn't display the system search dialog.
我也尝试设置搜索元数据,而不是主要活动的整个应用程序:
I have also tried setting the search metadata for the whole application as opposed to the main activity:
<manifest> <application> <activity ...MainApp... > ... </activity> <activity ...SearchActivity... > ... </activity> <meta-data android:name="android.app.default_searchable" android:value="org.homphysiology.lists.SearchActivity" /> </application> </manifest>同样的问题!搜索活动从不实例。
same problem! search activity is never instantiated.
推荐答案的 Android文档说:
请注意:在&lt;意图滤波器>不需要&下;类>的默认值(你通常看到上述&lt;活性>元素),因为系统明确地提供了ACTION_SEARCH意图的搜索活动,使用它的组件名称
Note: The < intent-filter> does not need a < category> with the DEFAULT value (which you usually see in < activity> elements), because the system delivers the ACTION_SEARCH intent explicitly to your searchable activity, using its component name.
编辑:
这是这样的:
<searchable xmlns:android="schamas.android/apk/res/android"应该是:
<searchable xmlns:android="schemas.android/apk/res/android"通知 schamas VS 模式:)
更多推荐
Android的搜索是永远不会调用
发布评论