无法在actionbar中获取searchview
我是java和android开发的新手; 我一直在开发一个应用程序,我需要一个带有操作栏的操作栏SearchView。我看过谷歌的例子,但我无法让它工作。我一定做错了,我即将放弃应用程序开发:DI已经搜索但我没有找到任何有用的东西。也许你们可以帮助我:)
问题:我得到的搜索视图应该打开,但在此之后什么也没发生,我注意到我的searchManager.getSearchableInfo(getComponentName())返回null。另外,我给出的提示没有显示在我的搜索框中,这让我相信应用程序可能无法找到我的搜索文件?够了:)
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>
这里是引用项目的链接,如果有人帮我这个,我将永远感激不尽!
尚方宝剑之说
相关分类