onCreateView() - 无法对非静态方法进行静态引用

onCreateView() - Cannot make a static reference to the non-static method

我试图在字段中输入一些文本后关闭软键盘。这是我在 Fragment 的 onCreateView() 上的代码。但是,我收到以下错误:

Cannot make a static reference to the non-static method getWindow() from the type Activity
Cannot make a static reference to the non-static method getSystemService(String) from the type Activity

有什么想法吗?

public class SignInActivity extends ActionBarActivity {

    [...]


    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_sign_in,
                    container, false);

            getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

            EditText phone =                   
              (EditText)getView().findViewById(R.id.input_field);
            phone.setOnEditorActionListener(new OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                        InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

                        in.hideSoftInputFromWindow(((TextView) v.getWindowToken()).getApplicationWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                       return true;

                    }
                    return false;
                }
            });

            return rootView;
        }
    }
}

需要

getActivity().getWindow()

getActivity().getSystemService()

您的代码中还有一个错误。而不是

EditText phone =                   
          (EditText)getView().findViewById(R.id.input_field);

需要

EditText phone =                   
          (EditText)rootView.findViewById(R.id.input_field);

这两种方法需要 Context 不包含哪个片段。

替换

getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

InputMethodManager in = (InputMethodManager)     getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);