title: Unable to access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()
body: <p>I've a fragment namely HomeFragment in my app. Following is the code for it:</p>
  <pre><code>import android.Manifest
  import android.app.Activity
  import android.app.AlertDialog
  import android.content.Intent
  import android.content.pm.PackageManager
  import androidx.lifecycle.ViewModelProvider
  import android.os.Bundle
  import androidx.fragment.app.Fragment
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import androidx.core.content.ContextCompat
  import androidx.fragment.app.viewModels
  import androidx.lifecycle.Observer
  import com.example.weatherapp.R
  import com.example.weatherapp.WeatherApplication
  import com.example.weatherapp.utils.GPS_REQUEST_CHECK_SETTINGS
  import com.example.weatherapp.utils.GpsUtil
  import com.example.weatherapp.utils.SharedPreferenceHelper
  import com.example.weatherapp.utils.observeOnce
  import com.google.android.material.snackbar.Snackbar
  import kotlinx.android.synthetic.main.home_fragment.view.*
  
  class HomeFragment : Fragment() {
  
      private lateinit var homeView: View
      private var isGPSEnabled = false
      private lateinit var prefs: SharedPreferenceHelper
  
      private val viewModel by viewModels&lt;HomeViewModel&gt; {
          HomeViewModel.HomeFragmentViewModelFactory(
              (requireContext().applicationContext as WeatherApplication).weatherRepository,
              requireActivity().application
          )
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          prefs = SharedPreferenceHelper.getInstance(requireContext())
          GpsUtil(requireContext()).turnGPSOn(object : GpsUtil.OnGpsListener {
              override fun gpsStatus(isGPSEnabled: Boolean) {
                  this@HomeFragment.isGPSEnabled = isGPSEnabled
              }
          })
      }
      override fun onStart() {
          super.onStart()
          invokeLocationAction()
      }
  
      private fun invokeLocationAction() {
          when {
              allPermissionsGranted() -&gt; {
                  viewModel.getLocationLiveData().observeOnce(
                      viewLifecycleOwner,
                      Observer { location -&gt;
                          if (location != null) {
                              viewModel.getWeather(location)
                          }
                      }
                  )
              }
  
              shouldShowRequestPermissionRationale() -&gt; {
                  AlertDialog.Builder(requireContext())
                      .setTitle(&quot;Location Permission&quot;)
                      .setMessage(&quot;This application requires access to your location to function!&quot;)
                      .setNegativeButton(&quot;No&quot;) { _, _ -&gt; requireActivity().finish() }
                      .setPositiveButton(&quot;Ask me&quot;) { _, _ -&gt;
                          requestPermissions(REQUIRED_PERMISSIONS, LOCATION_REQUEST_CODE)
                      }
                      .show()
              }
  
              !isGPSEnabled -&gt; {
                  Snackbar.make(
                      homeView.root,
                      &quot;GPS is required for this application to function!&quot;,Snackbar.LENGTH_SHORT
                  ).show()
              }
  
              else -&gt; {
                  requestPermissions(REQUIRED_PERMISSIONS, LOCATION_REQUEST_CODE)
              }
          }
      }
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?
      ): View? {
          homeView =  inflater.inflate(R.layout.home_fragment, container, false)
          return view;
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
          when (requestCode) {
              Activity.RESULT_OK -&gt; {
                  when (requestCode) {
                      GPS_REQUEST_CHECK_SETTINGS -&gt; {
                          isGPSEnabled = true
                          invokeLocationAction()
                      }
                  }
              }
  
              Activity.RESULT_CANCELED -&gt; {
                  when (requestCode) {
                      GPS_REQUEST_CHECK_SETTINGS -&gt; {
                          Snackbar.make(
                              homeView.root,&quot;Enable your GPS and restart!&quot;,Snackbar.LENGTH_LONG
                          ).show()
                      }
                  }
              }
          }
      }
  
      private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
          ContextCompat.checkSelfPermission(requireContext(), it) == PackageManager.PERMISSION_GRANTED
      }
  
      private fun shouldShowRequestPermissionRationale() = REQUIRED_PERMISSIONS.all {
          shouldShowRequestPermissionRationale(it)
      }
  
      override fun onRequestPermissionsResult(
          requestCode: Int, permissions: Array&lt;out String&gt;, grantResults: IntArray) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults)
          if (requestCode == LOCATION_REQUEST_CODE) {
              invokeLocationAction()
          }
      }
  
      companion object {
          private val REQUIRED_PERMISSIONS = arrayOf(
              Manifest.permission.ACCESS_FINE_LOCATION,
              Manifest.permission.ACCESS_COARSE_LOCATION
          )
          private const val LOCATION_REQUEST_CODE = 123
      }
  }
  </code></pre>
  <p>The app compiles successfully. But just as I open it on my device, the app crashes.
  And this is the error I'm getting in logcat upon running the app:</p>
  <pre><code>2021-05-25 17:45:25.968 32061-32061/com.example.weatherapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
  2021-05-25 17:45:25.989 32061-32061/com.example.weatherapp D/AndroidRuntime: Shutting down VM
      
      
      --------- beginning of crash
  2021-05-25 17:45:25.997 32061-32061/com.example.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.weatherapp, PID: 32061
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherapp/com.example.weatherapp.ui.MainActivity}: java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2949)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
          at android.app.ActivityThread.-wrap11(Unknown Source:0)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:200)
          at android.app.ActivityThread.main(ActivityThread.java:6956)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)
       Caused by: java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()
          at androidx.fragment.app.Fragment.getViewLifecycleOwner(Fragment.java:361)
          at com.example.weatherapp.ui.home.HomeFragment.invokeLocationAction(HomeFragment.kt:57)
          at com.example.weatherapp.ui.home.HomeFragment.onStart(HomeFragment.kt:50)
          at androidx.fragment.app.Fragment.performStart(Fragment.java:3021)
          at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
          at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
          at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
          at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
          at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
          at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:3079)
          at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:262)
          at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:510)
          at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1335)
          at android.app.Activity.performStart(Activity.java:7245)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2912)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) 
          at android.app.ActivityThread.-wrap11(Unknown Source:0) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:200) 
          at android.app.ActivityThread.main(ActivityThread.java:6956) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836) 
  2021-05-25 17:45:26.014 32061-32061/com.example.weatherapp I/Process: Sending signal. PID: 32061 SIG: 9
  </code></pre>
  <p>I assume I've tried to access lifecycleOwner of the fragment view in the wrong place. Please help me with this issue.</p>