[英]If ViewBinding and GoogleMap are used together, an error will occur
class class
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var viewBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(R.layout.activity_main)
}
layout布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</LinearLayout>
enter image description here在此处输入图像描述
If you write ViewBinding and GoogleMap together, an error will occur.如果把 ViewBinding 和 GoogleMap 一起写,会报错。 If you delete ViewBinding, no error will occur.
如果删除 ViewBinding,则不会出现错误。 What's the cause?
原因是什么?
This is how you are supposed to do ViewBinding with an Activity:这就是你应该如何使用 Activity 进行 ViewBinding:
private lateinit var binding: ResultProfileBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ResultProfileBinding.inflate(layoutInflater) val view = binding.root setContentView(view) } ```
As you can see, the resource id of the layout is not used, but the view from the binding instead.如您所见,没有使用布局的资源 id,而是使用绑定中的视图。
source: https://developer.android.com/topic/libraries/view-binding来源: https://developer.android.com/topic/libraries/view-binding
And then how you set the SupportMapFragment into the xml:然后是如何将 SupportMapFragment 设置到 xml 中:
<fragment class="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/>
Using class
instead of android:name
使用
class
代替android:name
source: https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment来源: https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment
There is nothing wrong with having SupportMapFragment and ViewBinding together.将 SupportMapFragment 和 ViewBinding 放在一起并没有错。
var viewBinding = ActivityMainBinding.inflate(layoutInflater) setContentView(R.layout.activity_main)
should be应该
val viewBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.