簡體   English   中英

onCreateView()-無法靜態引用非靜態方法

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

在字段中輸入一些文本后,我試圖關閉軟鍵盤。 這是片段上的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM