I have a layout with some TextView and a CardView. Only the reference binding.mycardview returns an object CardView?
, but according to 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.
My layout 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>
So, why RowBinding.tvLink
is an object TextView
marked with @NonNull
but RowBinding.cVComune
is a CardView?
marked @Nullable
? Is this a bug?
Do you have multiple versions of row.xml
? For example, res/layout/row.xml
and res/layout-land/row.xml
? If the CardView
isn't present in one of the two layout files, or if the android:id
attribute isn't on the CardView, its type will be nullable.
You ought to be able to view the generated binding file in Android Studio by looking inside your module's build/generated/data_binding_base_class_source_out
directory. That file should have a comment explaining why the view is nullable.
For example, here I deleted the android:id
attribute from my TextView
in the layout-land
version of my layout file:
/**
* 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;