无法获取操作栏中的searchview工作

我是相当新的Java和Android开发; 我一直在一个女巫的应用程序,我想要一个带有SearchView的操作栏。 我看了谷歌的例子,但我不能得到它的工作。 我一定在做错事,我即将放弃应用程序开发:DI已经search,但我还没有发现任何有用的东西。 也许你们可以帮我:)

问题 :我得到的search视图打开,因为它应该,但之后没有任何反应,我注意到我的searchManager.getSearchableInfo(getComponentName())返回null。 我的提示也没有显示在我的search框巫婆让我相信,也许应用程序找不到我的searchable.xml? 说够了:)

MainActivity.java

 public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); // Get the SearchView and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); return true; } } 

searchable.xml

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_label"> </searchable> 

Androidmanifest

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.searchapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SearchableActivity" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </application> </manifest> 

SearchableActivity.java

 package com.example.searchapp; import android.app.ListActivity; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class SearchableActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); // Get the intent, verify the action and get the query Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doMySearch(query); } } private void doMySearch(String query) { Log.d("Event",query); } } 

这里有一个链接到被引用的项目,我会永远感激,如果有人来帮助我这个!

源代码

你的问题在你的AndroidManifest中。 我几乎和你一样,因为这是我得到的文件。 但是不清楚或错误。

看着API演示源,我发现“android.app.searchable”元数据必须放在你的“结果”活动中,而在主要活动(或放置SearchView的活动)中,你指向另一个“ android.app.default_searchable”。

你可以在下面的文件中看到它,这是我testing项目中的Manifest:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <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=".SearchActivity" /> </activity> <activity android:name=".SearchActivity" android:label="@string/app_name" > <!-- This intent-filter identifies this activity as "searchable" --> <intent-filter> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <!-- This metadata entry provides further configuration details for searches --> <!-- that are handled by this activity. --> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> </application> 

searchable.xml中的提示和标签是引用,而不是硬编码的string也很重要。

我希望它能解决你的问题。 我花了一整天的时间弄清楚:(

Android指南明确指出:

 // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

这是你的情况不是真的。 因为您不想在MainActivity中search但在SearchableActivity中search。 这意味着你应该使用这个ComponentName而不是getComponentName()方法:

 ComponentName cn = new ComponentName(this, SearchableActivity.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); 

另外,似乎android:hint和android:label属性都必须是对strings.xml中string的引用。 懒惰和硬编码的string值似乎并没有工作:)

即不要做:

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="Search for something..." android:label="My App"> </searchable> 

但绝对要做(正如OP所做的那样):

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_label"> </searchable> 

如果您正在使用Android Studio,

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

部分可能不起作用。

编写完整的包名称

 <meta-data android:name="android.app.default_searchable" android:value="com.example.test.SearchActivity" /> 

小心“xml / searchable.xml”。 如果你用android:hintandroid:label做了HARD CODE,那么它将不起作用。 哈哈应该是:

 <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/app_name" android:label="@string/search_lb" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/> 

实际上没有必要使用上述答案中描述的“缺失部分”。 这个方法(使用SearchView )和官方文档一样,直到使用search小部件 。 这里稍作修改需要做如下:

SearchableInfo中的属性用于显示标签,提示,build议,创build启动search结果屏幕的意图,并控制其他function,如语音button( 文档 )。

因此,我们传递给SearchView.setSearchableInfo(SearchableInfo s)SearchableInfo对象必须包含对应该显示的目标活动的适当引用以显示search结果。 这是通过手动传递目标活动的ComponentName来完成的。 一个用于创buildComponentName构造函数是

 public ComponentName (String pkg, String cls) 

pkgcls是要显示search结果的目标活动的包名和类名。 注意:

pkg必须指向项目的根包名称,

cls必须是一个完全合格的class级名称(即整个事物)

例如:

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); // Get the SearchView and set the searchable configuration String pkg = "com.example.musicapp" String cls = "com.example.musicapp.search.SearchActivity" ComponentName mycomponent = new ComponentName(pkg,cls); SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(mycomponent)); searchView.setIconifiedByDefault(false); //if using on actionbar return true; } 

根据您的要求,看看其他的ComponentName构造函数。

请确保您的searchables.xml文件位于正确的位置(即在res/xml/文件夹中),并且相同的文件不包含任何错误 – 否则您会发现(SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName)将返回null,从而中断searchfunction。

(另外,请确保以适当的方式设置componentName ,因为Android指南中显示的示例仅适用于在同一活动中进行search和显示search的情况。)

仔细检查在values / strings.xml中是否存在相应的条目(即search_hint和app_label)

例如

  <string name="app_label">FunApp</string> <string name="search_hint">Caps Off</string>