【问题标题】:Search in Android not working在 Android 中搜索不起作用
【发布时间】:2014-05-04 16:58:36
【问题描述】:

我几天来一直在努力使用 android 的搜索功能。无法查明错误。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exclude_list);

    .......

    SearchView searchView = (SearchView)findViewById(R.id.options_menu_main_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

    handleIntent(getIntent());

}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.i("q",query);
    }
}

可搜索的.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
 android:hint="@string/search_hint"
 android:includeInGlobalSearch="false"
 android:label="@string/app_name"
 android:searchSettingsDescription="@string/search_hint" />

菜单.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item
        android:id="@+id/options_menu_main_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="always"
        android:title="Search"/>
</menu>

清单

<activity
 android:name="com.xyz.Exclude_list"
 android:label="@string/title_activity_exclude_list"
 android:screenOrientation="portrait"
 android:launchMode="singleTop">
 <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
    <action android:name="android.intent.action.VIEW" />
 </intent-filter>

 <meta-data
  android:name="android.app.searchable"
  android:resource="@xml/searchable"
  android:value=".app.Search"
 />
</activity>

错误

Caused by: java.lang.NullPointerException
  at com.xyz.Exclude_list.onCreate(Exclude_list.java:64)

第 64 行:searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

如果我删除

 SearchView searchView = (SearchView)findViewById(R.id.options_menu_main_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

搜索正常,但是当我在搜索中输入时,我无法获取/记录任何查询。

【问题讨论】:

  • 第 64 行 Exclude_list.java 上有什么内容?
  • searchView 必须为空。您确定它在 activity_exclude_list 布局中并且具有 id:options_menu_main_search
  • 你使用ActionBarActivity作为活动扩展吗?
  • @Fllo: no.Just public class Exclude_list extends Activity{}
  • @curious_coder 更改不会产生任何影响,因为您有 NPE。您需要在onCreateOptionsMenu 中初始化搜索视图。阅读相同的@developer.android.com/guide/topics/ui/actionbar.html

标签: java android nullpointerexception android-searchmanager


【解决方案1】:

您正在尝试在 onCreate 方法中初始化 MenuItem。您应该在 onCreateOptionsMenu 方法中将其初始化为:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.options_menu_main_search).getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
}  

请参阅文档中的Using Search Widget 主题,特别是配置搜索小部件部分。另外,因为你没有使用AppCompat 库,你应该使用这个类(在the docs 中声明):

android:actionViewClass="android.widget.SearchView"

【讨论】:

  • 非常感谢!!它现在正在工作。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 2017-02-16
  • 2011-09-19
相关资源
最近更新 更多