Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 活动启动后激活ActionBar搜索_Android_Android Actionbar - Fatal编程技术网

Android 活动启动后激活ActionBar搜索

Android 活动启动后激活ActionBar搜索,android,android-actionbar,Android,Android Actionbar,我正在创建一个定制的AndroidActionBarsearch follow handleMenuSearch()在选择的选项项上激活 protected void handleMenuSearch(){ ActionBar action = getSupportActionBar(); //get the actionbar if(isSearchOpened){ //test if the search is open action.setDisplay

我正在创建一个定制的Android
ActionBar
search follow

handleMenuSearch()在选择的选项项上激活

protected void handleMenuSearch(){
    ActionBar action = getSupportActionBar(); //get the actionbar

    if(isSearchOpened){ //test if the search is open

        action.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
        action.setDisplayShowTitleEnabled(true); //show the title in the action bar

        //hides the keyboard
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edtSeach.getWindowToken(), 0);

        //add the search icon in the action bar
        mSearchAction.setIcon(getResources().getDrawable(R.drawable.search));

        isSearchOpened = false;
    } else { //open the search entry

        action.setDisplayShowCustomEnabled(true); //enable it to display a
        // custom view in the action bar.
        action.setCustomView(R.layout.search_bar);//add the custom view
        action.setDisplayShowTitleEnabled(false); //hide the title

        edtSeach = (EditText)action.getCustomView().findViewById(R.id.edtSearch); //the text editor

        edtSeach.requestFocus();

        edtSeach.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                loadData(edtSeach.getText().toString());
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            public void afterTextChanged(Editable s) {

            }
        });

        //open the keyboard focused in the edtSearch
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(edtSeach, InputMethodManager.SHOW_IMPLICIT);


        //add the close icon
        mSearchAction.setIcon(getResources().getDrawable(R.drawable.ximage));

        isSearchOpened = true;
    }
}
我想要实现的是在活动开始后激活搜索

我试着把
handleMenuSearch()
onCreate
下,但这会给我
NullPointerException

我想这是因为我无法从
onCreate
调用
mSearchAction

public boolean onPrepareOptionsMenu(Menu menu) {
        mSearchAction = menu.findItem(R.id.search);
        return super.onPrepareOptionsMenu(menu);
    }

使用
处理程序解决问题

protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                handleMenuSearch();
            }
        }, 1000);
    }

将其放入onPrepareOptions菜单mSearchAction.setDisplayShowCustomEnabled(true)中;我遇到“无法解决方法”错误。请查看此链接谢谢,但我没有使用SearchView,我使用的是自定义工具栏。