为什么使用绑定 class 的视图引用标记为 @Nullable?

Why reference of a view using a binding class is marked with @Nullable?

我有一个包含一些 TextView 和一个 CardView 的布局。仅引用binding.mycardviewreturns一个对象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 是标有 @NonNull 的对象 TextViewRowBinding.cVComune 是标有 @NullableCardView??这是一个错误吗?

你有多个版本的row.xml吗?例如,res/layout/row.xmlres/layout-land/row.xml?如果 CardView 不存在于两个布局文件之一中,或者如果 android:id 属性不在 CardView 上,则其类型将为空。

您应该能够通过查看模块的 build/generated/data_binding_base_class_source_out 目录在 Android Studio 中查看生成的绑定文件。该文件应该有一条注释,解释为什么视图可以为空。

例如,这里我从布局文件的 layout-land 版本中的 TextView 中删除了 android:id 属性:

  /**
   * 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;