getActionView返回null

时间:2014-08-27 14:20:09

标签: android android-fragmentactivity

我正在从FragmentActivity扩展主要活动。我得到了getActionView null。我该如何解决这个问题?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    searchItem = menu.findItem(R.id.action_search);
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
    searchView = (SearchView)searchItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setQueryHint(getResources().getString(R.string.hint)); //if you want a custom hint
    searchView.setOnQueryTextListener(this);
    EditText et = (EditText)searchView.findViewById(searchView.getContext().getResources()
            .getIdentifier("android:id/search_src_text", null, null));
    et.setHintTextColor(getResources().getColor(R.color.blue)); // if you want to change hint color
    et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(25) }); // if you want to set max. text limit
    return true;
}

menu.xml文件

  <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="Ara"
      android:showAsAction="always|collapseActionView" 
      app:actionViewClass="android.widget.SearchView"/>

1 个答案:

答案 0 :(得分:1)

请尝试使用此代码:

android:actionViewClass="android.widget.SearchView"