Android搜索从未被调用

3
我完全被卡住了,已经花了几个小时阅读可能的解决方案。我无法让我的搜索活动开始。
我有一个主要活动,我希望从中搜索按钮启动搜索活动:
文件 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()函数,并返回trueAndroidManifest.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>

相同的问题!搜索活动从未被实例化。
2个回答

1

请尝试这个:

Intent i = new Intent("android.intent.action.SEARCH");

startActivity(i);


好的,我现在已经删除了那个属性。但是问题依旧!搜索功能无法启动...谢谢! - Sanjay Manohar
你能简单地告诉我你的确切问题是什么吗? - Viswanath Lekshmanan
应用程序执行正常。 我希望在选择菜单选项或按下硬件搜索键时启动“SearchActivity”。 目前,在这两种情况下都没有任何反应。 :-( - Sanjay Manohar
onsearchrequested() 是您想要执行的方法吗?您说它执行得很好并返回了 true。您能发一下该方法的代码吗? - Viswanath Lekshmanan
我没有修改默认的onSearchRequested()方法。我相信它是android.app.Activity的一部分,它应该创建在清单文件中指定的默认搜索对话框?文档说如果搜索对话框被创建,则会返回true(http://developer.android.com/reference/android/app/Activity.html#onSearchRequested%28%29)。但是我的`SearchActivity`没有被创建! - Sanjay Manohar
这段代码 Intent("android.intent.action.SEARCH"); 会弹出一个菜单,用于搜索手机上安装的许多其他应用程序。但是使用您的技术,我能够手动实例化自己的 SearchActivity(请参见主帖的编辑)。不幸的是,这种方法无法弹出搜索对话框... - Sanjay Manohar

1

Android文档中指出:

注意:<intent-filter>不需要带有DEFAULT值的<category>(通常在<activity>元素中看到),因为系统会明确地将ACTION_SEARCH意图传递给可搜索的活动,并使用其组件名称。

编辑:

是这个:

<searchable xmlns:android="http://schamas.android.com/apk/res/android"

应该是:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"

注意:schamas和schemas的区别 :)

谢谢!我已经尝试删除<category name=DEFAULT>,但没有成功!同样的问题,搜索按钮没有反应,LOGCAT中也没有任何信息。 - Sanjay Manohar
1
你在SearchActivityonCreate方法里做日志记录,还是只是使用断点调试?你可以尝试添加日志以确保操作是否正确。 - Tushar
好的,我已经尝试在各个地方记录日志了,它确实调用了MainApponSearchRequested()方法,但从未实例化SearchActivity类... 这是个谜? - Sanjay Manohar
1
@SanjayManohar,你能加入聊天室的讨论吗?http://chat.stackoverflow.com/rooms/26799/help-sanjay - Tushar
非常感谢 @Tushar,就是这个!只需要用 'schema'。现在完美运行了! - Sanjay Manohar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接