本文介绍了MenuItemCompat.getActionView总是返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚实施的 V7 AppCompat 支持库,但 MenuItemCompat.getActionView 总是在每一个Android版本返回null我测试(4.2.2,2.3.4 ....)

搜索查看显示在操作栏中,但它不响应触摸操作,不扩张,以显示其的EditText 键,就像是一个简单的图标。

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.menu,菜单);

    菜单项searchItem = menu.findItem(R.id.action_search);
    搜索查看搜索查看=(搜索查看)MenuItemCompat.getActionView(searchItem);
    如果(搜索查看!= NULL){
        SearchViewCompat.setOnQueryTextListener(搜索查看,mOnQueryTextListener);
        sea​​rchView.setIconifiedByDefault(假);
        Log.d(TAG,搜索查看非空);
    } 其他
        Log.d(TAG,搜索查看为空);
    }
    返回super.onCreateOptionsMenu(菜单);
}
 

menu.xml文件

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>

    <项目机器人:ID =@ + ID / ACTION_SEARCH
          应用程序:showAsAction =永远| collapseActionView
          机器人:图标=@可绘制/ abc_ic_search
          机器人:标题=@字符串/ action_bar_search
          机器人:actionViewClass =android.support.v7.widget.SearchView/>

    <项目机器人:ID =@ + ID / action_refresh
          机器人:图标=@可绘制/刷新
          机器人:标题=@字符串/ action_bar_refresh
          应用程序:showAsAction =ifRoom/>
< /菜单>
 

解决方案

最后,我找到了解决办法。

  1. 改变 actionViewClass 的命名空间,安卓actionViewClass 应用程序:actionViewClass

  2. 实施 android.support.v7.widget.SearchView.OnQueryTextListener 接口,用于当前的活动。

  3. 直接用 setOnQueryTextListener 而不是 SearchViewCompat.setOnQueryTextListener

      @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
      MenuInflater充气= getMenuInflater();
      inflater.inflate(R.menu.menu,菜单);
    
      菜单项searchItem = menu.findItem(R.id.action_search);
      搜索查看搜索查看=(搜索查看)MenuItemCompat.getActionView(searchItem);
      如果(搜索查看!= NULL){
         sea​​rchView.setOnQueryTextListener(本);
      }
    
      返回super.onCreateOptionsMenu(菜单);
    }
     

I just implemented the v7 AppCompat support library but the MenuItemCompat.getActionView always return null in every Android version I tested (4.2.2, 2.3.4 ....)

The SearchView is displayed in action bar but it doesn't respond to touch actions and doesn't expand to show its EditText and is just like a simple icon.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    if (searchView != null) {
        SearchViewCompat.setOnQueryTextListener(searchView, mOnQueryTextListener);
        searchView.setIconifiedByDefault(false);
        Log.d(TAG,"SearchView not null");
    } else
        Log.d(TAG, "SearchView is null");
    }
    return super.onCreateOptionsMenu(menu);
}

Menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_search"
          app:showAsAction="always|collapseActionView"
          android:icon="@drawable/abc_ic_search"
          android:title="@string/action_bar_search"
          android:actionViewClass="android.support.v7.widget.SearchView"/>

    <item android:id="@+id/action_refresh"
          android:icon="@drawable/refresh"
          android:title="@string/action_bar_refresh"
          app:showAsAction="ifRoom"/>
</menu>
解决方案

Finally I found the solution.

  1. Changing namespace of actionViewClass from android:actionViewClass to app:actionViewClass

  2. Implementing android.support.v7.widget.SearchView.OnQueryTextListener interface for current activity.

  3. Directly use setOnQueryTextListener instead of SearchViewCompat.setOnQueryTextListener

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
    
      MenuItem searchItem = menu.findItem(R.id.action_search);
      SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
      if (searchView != null) {
         searchView.setOnQueryTextListener(this);
      }
    
      return super.onCreateOptionsMenu(menu);
    }
    

这篇关于MenuItemCompat.getActionView总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:09