Android操作栏搜索无法正常工作

时间:2013-12-31 15:33:59

标签: android-listview android-actionbar android-search

我想使用我实现的getfilter(),但是从操作栏中搜索我的listview。但不知道为什么应用程序继续给nullpointer我也无法理解错误。 这是我的代码调用我的自定义getfilter在我的coustom适配器中实现。

public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    ListView itcItems;
    DataBaseHandler db;
    private SearchView mSearchView;
    //private EditText mStatusView;

    public DummySectionFragment() {
    }

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

         //MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); 

        //getMenuInflater().inflate(R.menu.action, menu);   


        searchView = (SearchView) menu.findItem(R.id.Search).getActionView();
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); 
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setIconifiedByDefault(false);   

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
        {
            @Override
            public boolean onQueryTextChange(String newText) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(newText);
                return true;
            }
            @Override
            public boolean onQueryTextSubmit(String query) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);





    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        setHasOptionsMenu(true);

        View rootView = inflater.inflate(R.layout.common_listview,
                container, false);
        setHasOptionsMenu(true);


        ActionBar actionbar = getActivity().getActionBar();
        getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        actionbar.show();
         //getActivity().requestWindowFeature(Window.FEATURE_ACTION_BAR);
         //mStatusView = (EditText) rootView.findViewById(R.id.status_text);
        itcItems = (ListView) rootView.findViewById(R.id.streamList);



        /*MyAsyncTask task = new MyAsyncTask(getActivity());
        task.execute("http://findaway.in/card/restlist.xml");


        *
        */
        db = new DataBaseHandler(getActivity());

        if(isOnline(getActivity()))

        { 
            flag=1;
            db.delete();
            getDataInAsyncTask();

            getImage(db);
            //db.close();

        }

        else
        {
            flag=0;
            Toast.makeText(getActivity(), "No internet",
                    Toast.LENGTH_LONG).show();

            try {
                db.openDataBase();
            } catch (SQLException sqle) {
                throw sqle;
            }

            getDataInDataBase(db);


        }

这是我的logcat enter image description here

第212行是searchView =(SearchView)menu.findItem(R.id.Search).getActionView();

还必须实现res / xml / searchable.xml吗?我做过那个

1 个答案:

答案 0 :(得分:0)

在onCreateOptionsMenu()中,缺少对super的调用。考虑使用super然后使用inflater和菜单对象。