我完全被卡住了,已经花了几个小时阅读可能的解决方案。我无法让我的搜索活动开始。
我有一个主要活动,我希望从中搜索按钮启动搜索活动:
文件 MainApp.java
无论按下用户界面按钮还是硬件搜索键,都没有唤起搜索。确实调用了
我可以翻译成中文:
这些字符串位于
搜索活动本身很简单,但从未被创建——我在构造函数里设置了断点:
文件
有人能够发现这里的问题吗? [在eclipse Juno 4.2.1中编译,SDK版本为21.0.1,在HTC desire Z上进行测试] 谢谢...
返回 null。也许这只是强调我的 SearchActivity 没有被正确注册的事实...
修改了 @arju 的建议: 我可以通过使用手动启动搜索活动
当然,这种方法无法使用硬件按钮,并且它也不会显示系统搜索对话框。
我还尝试了为整个应用程序设置搜索元数据,而不是主要活动:
相同的问题!搜索活动从未被实例化。
我有一个主要活动,我希望从中搜索按钮启动搜索活动:
文件 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;
}
}
无论按下用户界面按钮还是硬件搜索键,都没有唤起搜索。确实调用了
onSearchRequested()
函数,并返回true
。
AndroidManifest.xml
文件:<?xml version="1.0" encoding="utf-8"?>
<manifest package="org.homphysiology.lists"
android:versionCode="1" android:versionName="1.0"
xmlns:android="http://schemas.android.com/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="http://schamas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
/>
这些字符串位于
/res/values/strings.xml
中。搜索活动本身很简单,但从未被创建——我在构造函数里设置了断点:
文件
SearchActivity.java
:public class SearchActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setListAdapter(...)
}
}
有人能够发现这里的问题吗? [在eclipse Juno 4.2.1中编译,SDK版本为21.0.1,在HTC desire Z上进行测试] 谢谢...
附录
((SearchManager)getSystemService(Context.SEARCH_SERVICE))
.getSearchableInfo(getComponentName())
返回 null。也许这只是强调我的 SearchActivity 没有被正确注册的事实...
修改了 @arju 的建议: 我可以通过使用手动启动搜索活动
boolean onOptionsItemSelected(MenuItem m){
startActivity(new Intent(this,SearchActivity.class));
return true;
}
当然,这种方法无法使用硬件按钮,并且它也不会显示系统搜索对话框。
我还尝试了为整个应用程序设置搜索元数据,而不是主要活动:
<manifest>
<application>
<activity ...MainApp... > ... </activity>
<activity ...SearchActivity... > ... </activity>
<meta-data
android:name="android.app.default_searchable"
android:value="org.homphysiology.lists.SearchActivity"
/>
</application>
</manifest>
相同的问题!搜索活动从未被实例化。
onSearchRequested()
方法。我相信它是android.app.Activity
的一部分,它应该创建在清单文件中指定的默认搜索对话框?文档说如果搜索对话框被创建,则会返回true
(http://developer.android.com/reference/android/app/Activity.html#onSearchRequested%28%29)。但是我的`SearchActivity`没有被创建! - Sanjay ManoharIntent("android.intent.action.SEARCH");
会弹出一个菜单,用于搜索手机上安装的许多其他应用程序。但是使用您的技术,我能够手动实例化自己的SearchActivity
(请参见主帖的编辑)。不幸的是,这种方法无法弹出搜索对话框... - Sanjay Manohar