gpt4 book ai didi

android - 为什么使用绑定(bind)类的 View 引用标有@Nullable?

转载 作者:行者123 更新时间:2023-12-04 07:16:15 28 4
gpt4 key购买 nike

我有一些 TextView 和 CardView 的布局。只有引用 binding.mycardview 返回一个对象 CardView? , 但根据 docs :

Null safety: Since view binding creates direct references to views, there's no risk of a null pointer exception due to an invalid view ID. Additionally, when a view is only present in some configurations of a layout, the field containing its reference in the binding class is marked with @Nullable.


我的布局 row.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:id="@+id/cVComune"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:layout_marginEnd="@dimen/space"
android:layout_marginBottom="@dimen/space"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvComune"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:textSize="@dimen/text_label_medium"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvCap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:textSize="@dimen/text_label_medium"
app:layout_constraintStart_toEndOf="@+id/ivMail"
app:layout_constraintTop_toBottomOf="@+id/tvComune" />

<TextView
android:id="@+id/tvPrefisso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:textSize="@dimen/text_label_medium"
app:layout_constraintStart_toEndOf="@+id/ivPhone"
app:layout_constraintTop_toBottomOf="@+id/tvComune" />

<TextView
android:id="@+id/tvAbitanti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:textSize="@dimen/text_label_medium"
app:layout_constraintStart_toEndOf="@+id/ivAbitanti"
app:layout_constraintTop_toBottomOf="@+id/tvComune" />

<TextView
android:id="@+id/tvLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:textColor="@color/hyperlink"
android:textSize="@dimen/text_label_medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvCap" />

<TextView
android:id="@+id/tvProvincia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/space"
android:layout_marginEnd="@dimen/space"
android:textSize="@dimen/text_label_medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvRegione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/space"
android:layout_marginEnd="@dimen/space"
android:textSize="@dimen/text_label_medium"
app:layout_constraintEnd_toStartOf="@+id/tvProvincia"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/ivMail"
android:layout_width="@dimen/widthimg"
android:layout_height="0dp"
android:layout_marginStart="@dimen/space"
android:layout_marginTop="@dimen/space"
android:contentDescription="@string/codicepostale"
app:layout_constraintBottom_toBottomOf="@+id/tvCap"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvComune"
app:layout_constraintVertical_bias="0.3"
app:srcCompat="@drawable/mail" />

<ImageView
android:id="@+id/ivPhone"
android:layout_width="@dimen/widthimg"
android:layout_height="0dp"
android:layout_marginStart="@dimen/spaceimg"
android:layout_marginTop="@dimen/space"
android:contentDescription="@string/telefono"
app:layout_constraintBottom_toBottomOf="@+id/tvCap"
app:layout_constraintStart_toEndOf="@+id/ivMail"
app:layout_constraintTop_toBottomOf="@+id/tvComune"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/phone" />

<ImageView
android:id="@+id/ivAbitanti"
android:layout_width="@dimen/widthimg"
android:layout_height="0dp"
android:layout_marginStart="@dimen/spaceimg"
android:layout_marginTop="@dimen/space"
android:contentDescription="@string/abitanti"
app:layout_constraintBottom_toBottomOf="@+id/tvPrefisso"
app:layout_constraintStart_toEndOf="@+id/ivPhone"
app:layout_constraintTop_toBottomOf="@+id/tvComune"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/stick" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>
那么,为什么 RowBinding.tvLink是一个对象 TextView标有 @NonNull但是 RowBinding.cVComuneCardView?标记为 @Nullable ?这是一个错误吗?

最佳答案

你有多个版本的row.xml ?例如,res/layout/row.xmlres/layout-land/row.xml ?如果 CardView两个布局文件之一中不存在,或者如果 android:id属性不在 CardView 上,它的类型可以为空。
您应该能够通过查看模块的build/generated/data_binding_base_class_source_out 来查看Android Studio 中生成的绑定(bind)文件。目录。该文件应该有一个注释,解释为什么 View 可以为空。
比如这里我删除了android:id来自我的 TextView 的属性在 layout-land我的布局文件的版本:

  /**
* This binding is not available in all configurations.
* <p>
* Present:
* <ul>
* <li>layout/</li>
* </ul>
*
* Absent:
* <ul>
* <li>layout-land/</li>
* </ul>
*/
@Nullable
public final TextView text;

关于android - 为什么使用绑定(bind)类的 View 引用标有@Nullable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68733772/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com