我正在尝试在ActionBar错误中集成SearchView(MenuItem1无法转换为android.widget.



我正在尝试在ActionBar中集成SearchView。我尝试了所有的教程,但还是没能成功。这个错误一直在出现(java.lang.ClassCastException:com.android.internal.view.menu.MenuItem1无法转换为android.widget.SearchView)。如果有人能帮我,我两天以来一直在找这个问题,那将非常有帮助。。

 import android.app.SearchManager;
 import android.support.v4.app.Fragment;//I have used Fragment
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.SearchView;

    // Created Options Menu 
    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
        inflater.inflate(R.menu.activity_main_actionmenu, menu);
        SearchManager searchManager=(SearchManager)   
getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView=(SearchView) menu.findItem(R.id.actionSearch);
// The error of java.lang.ClassCastException: com.android.internal.view.menu.MenuItem1 cannot  
 //be cast to android.widget.SearchView occur because of above line
searchView.setSearchableInfo(searchManager
        .getSearchableInfo(getActivity().getComponentName()));
super.onCreateOptionsMenu(menu,inflater);
}

MenuItem1.xml

<item
android:id="@+id/actionSearch"
android:actionViewClass="android.widget.SearchView"
// I think android.support.v4.app.Fragment doesnot support android.widget.SearchView
android:icon="@drawable/ic_action_search"
android:showAsAction="always"/>

尝试删除import library android.widget.SearchView,当您再次传递光标导入它时,验证您是否有更多选项。

误解是做Cast(SearchManager)是一个不是你类型的对象,如果你已经使用,就试试这个

SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem (R.id.actionSearch) .getActionView ();

相关内容

  • 没有找到相关文章

最新更新