Android 在第一次远程调用时显示空值的数据绑定

Android 在第一次远程调用时显示空值的数据绑定,android,kotlin,android-databinding,android-mvvm,Android,Kotlin,Android Databinding,Android Mvvm,在我的应用程序中,我有一个片段调用远程服务来获取用户配置文件信息并显示它,我使用数据绑定来显示数据 这是我的布局: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data>

在我的应用程序中,我有一个片段调用远程服务来获取用户配置文件信息并显示它,我使用数据绑定来显示数据

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="viewModel"
            type="com.myapp.ProfileViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{viewModel.profile.firstName+ ' '+ viewModel.profile.lastName}" />

            <!-- Other textviews -->

    </LinearLayout>

</layout>

现在,当我第一次打开片段时,存储库调用服务并正确地获取数据,但文本视图中显示“null”。若我关闭片段并重新打开它,edittext将正确填充。我的代码怎么了

在片段类中设置binding.lifecycleOwner,以便在相应视图中反映LiveData对象中的更新。您的片段类应该如下所示:

class ProfileFragment : Fragment() {

    private lateinit var binding: FragmentProfileBinding
    private lateinit var viewModel: ProfileViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_profile,
            container,
            false
        )

        viewModel = activity?.run {
            ViewModelProviders.of(this)[ProfileViewModel::class.java]
        } ?: throw Exception("Invalid Activity")

        binding.viewModel = viewModel 

        //add lifecycleOwner
        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.getProfile(
            "aToken"
        )
    }
}

使用
binding.lifecycleOwner=viewLifecycleOwner
让视图处理
LiveData
class ProfileFragment : Fragment() {

    private lateinit var binding: FragmentProfileBinding
    private lateinit var viewModel: ProfileViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_profile,
            container,
            false
        )

        viewModel = activity?.run {
            ViewModelProviders.of(this)[ProfileViewModel::class.java]
        } ?: throw Exception("Invalid Activity")

        binding.viewModel = viewModel

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.getProfile(
            "aToken"
        )
    }
}
class ProfileFragment : Fragment() {

    private lateinit var binding: FragmentProfileBinding
    private lateinit var viewModel: ProfileViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_profile,
            container,
            false
        )

        viewModel = activity?.run {
            ViewModelProviders.of(this)[ProfileViewModel::class.java]
        } ?: throw Exception("Invalid Activity")

        binding.viewModel = viewModel 

        //add lifecycleOwner
        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.getProfile(
            "aToken"
        )
    }
}