当 getView() 为 null 时,即 onCreateView() 之前或 onDestroyView() 之后,无法访问 Fragment View 的 LifecycleOwner [重复

Posted

技术标签:

【中文标题】当 getView() 为 null 时,即 onCreateView() 之前或 onDestroyView() 之后,无法访问 Fragment View 的 LifecycleOwner [重复]【英文标题】:Unable to access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView() [duplicate] 【发布时间】:2021-08-13 17:21:34 【问题描述】:

我的应用中有一个片段,即 HomeFragment。以下是它的代码:

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<HomeViewModel> 
        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() -> 
                viewModel.getLocationLiveData().observeOnce(
                    viewLifecycleOwner,
                    Observer  location ->
                        if (location != null) 
                            viewModel.getWeather(location)
                        
                    
                )
            

            shouldShowRequestPermissionRationale() -> 
                AlertDialog.Builder(requireContext())
                    .setTitle("Location Permission")
                    .setMessage("This application requires access to your location to function!")
                    .setNegativeButton("No")  _, _ -> requireActivity().finish() 
                    .setPositiveButton("Ask me")  _, _ ->
                        requestPermissions(REQUIRED_PERMISSIONS, LOCATION_REQUEST_CODE)
                    
                    .show()
            

            !isGPSEnabled -> 
                Snackbar.make(
                    homeView.root,
                    "GPS is required for this application to function!",Snackbar.LENGTH_SHORT
                ).show()
            

            else -> 
                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 -> 
                when (requestCode) 
                    GPS_REQUEST_CHECK_SETTINGS -> 
                        isGPSEnabled = true
                        invokeLocationAction()
                    
                
            

            Activity.RESULT_CANCELED -> 
                when (requestCode) 
                    GPS_REQUEST_CHECK_SETTINGS -> 
                        Snackbar.make(
                            homeView.root,"Enable your GPS and restart!",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<out String>, 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
    

应用程序编译成功。但就在我在我的设备上打开它时,应用程序崩溃了。 这是我在运行应用程序时在 logcat 中遇到的错误:

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 ComponentInfocom.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

我假设我尝试在错误的位置访问片段视图的生命周期所有者。请帮我解决这个问题。

【问题讨论】:

【参考方案1】:

您的onCreateView() 没有返回您刚刚膨胀的视图。这意味着您的 Fragment 的 View 为空,viewLifecycleOwner 确实无效。

您应该更改您的onCreateView 以返回您新膨胀的视图:

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 
        homeView = inflater.inflate(R.layout.home_fragment, container, false)
        return homeView
    

【讨论】:

以上是关于当 getView() 为 null 时,即 onCreateView() 之前或 onDestroyView() 之后,无法访问 Fragment View 的 LifecycleOwner [重复的主要内容,如果未能解决你的问题,请参考以下文章

从活动创建片段时getView返回null

创建您自己的自定义适配器时,getView() 方法如何工作?

SET ANSI_NULLS ON

android - Fragment getView() 总是返回null

滚动时显示随机项目的getView

我无法写入 EditText,当我尝试写东西时它消失了,这是因为当我修改数据时调用了 getView()