getActivity() returns null in Fragment function

后端 未结 15 1936
别那么骄傲
别那么骄傲 2020-11-22 07:28

I have a fragment (F1) with a public method like this

public void asd() {
    if (getActivity() == null) {
        Log.d(\"yes\",\"it is null\");
    }
}
         


        
15条回答
  •  星月不相逢
    2020-11-22 08:27

    Where do you call this function? If you call it in the constructor of Fragment, it will return null.

    Just call getActivity() when the method onCreateView() is executed.

提交回复
热议问题