getView returning null when fragment has been created from an activity getView returning null when fragment has been created from an activity android android

getView returning null when fragment has been created from an activity


Move your method call to be executed during onCreateView, and use the view you are inflating for reference instead of getView(). See the fragment lifecycle for more information: https://developer.android.com/guide/components/fragments.html#Creating

and the documentation of getView() that explains why it returns null before onCreateView(LayoutInflater, ViewGroup, Bundle) returns:

getView() Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.

https://developer.android.com/reference/android/app/Fragment.html#getView()


Moving the method to onCreateView() did not help me.so... Create a global variable mView

protected View mView;

and in onCreateView()

@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  Log.d(TAG, "oncreateView");  super.onCreateView(inflater, container, savedInstanceState);  View view = inflater.inflate(R.layout.activity_secure_cloud_drive_folder, container, false);  this.mView = view;  return view;}

and the replace all your getView() with mView


You can fix that by putting your code inside the onViewCreated()-method, which you should override. Don't forget to call super() on it.