使用视图绑定时,如何处理不同布局配置中缺失的视图?

Ste*_*ang 4 android android-button kotlin android-viewbinding

据我所知,任何带有 ID 的 XML 元素都应该通过视图绑定自动拉入活动类。但是,Android Studio 不断提示我的按钮可以为 null,并且需要具有?.!!.进行编译。

如果我用 断言不可为 null !!.,它实际上会导致按钮出现运行时 NullPointerException。

为什么视图绑定无法识别我的按钮的类型和存在?

我的主要活动代码在这里:

package com.stevenswang.ageinminutes

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.stevenswang.ageinminutes.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.btnSelectDate.setOnClickListener {...} //this line gives the compiler error in the title
    }
    
}
Run Code Online (Sandbox Code Playgroud)

此处应用程序的 XML(完整):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:padding="16sp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Caculate Your"
        android:textColor="@color/headingColor"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Age"
        android:textColor="@color/white"
        android:background="@color/purple_200"
        android:padding="10sp"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvInMinutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="In Minutes"
        android:textColor="@color/headingColor"
        android:textSize="30sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnSelectDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Date"
        android:layout_marginTop="10sp"
        android:textSize="20sp"
        android:backgroundTint="@color/buttonColor"
        />

    <TextView
        android:id="@+id/tvSelectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textColor="@color/headingColor"
        android:textSize="20sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Selected Date"
        android:layout_marginTop="2dp"
        android:textColor="@color/gray"
        android:textSize="20sp"
        />

    <TextView
        android:id="@+id/tvAgeInMinutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColor="@color/headingColor"
        android:textSize="36sp"
        android:textStyle="bold"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age in minutes"
        android:layout_marginTop="2dp"
        android:textColor="@color/gray"
        android:textSize="24sp"
        />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我找出了罪魁祸首,在视图绑定文档上它说:

空安全:由于视图绑定创建对视图的直接引用,因此不存在因视图 ID 无效而导致空指针异常的风险。此外,当视图仅出现在布局的某些配置中时,绑定类中包含其引用的字段将用 @Nullable 标记

来源: https: //developer.android.com/topic/libraries/view-binding#findviewbyid

我的按钮中的一项规则仅在 API 21 或更高版本中使用,我当前的最小值设置为 16(请参见下面的屏幕截图)。我遵循 Android Studio 的建议并覆盖版本,从而创建了 2 个单独的activity_main.xml. 这就是触发可为空行为的原因。

API 限制创建多个 xml 文件

当我删除 v21 xml 文件时,可空值消失了。

现在,在我向阅读本文的人提出的后续问题中,为什么断言不可为空仍然会导致应用程序崩溃?处理多个activity_main.xml版本的正确方法是什么?

提前致谢!

Ste*_*ang 6

我找出了罪魁祸首,在视图绑定文档上它说:

空安全:由于视图绑定创建对视图的直接引用,因此不存在因视图 ID 无效而导致空指针异常的风险。此外,当视图仅出现在布局的某些配置中时,绑定类中包含其引用的字段将用 @Nullable 标记

来源: https: //developer.android.com/topic/libraries/view-binding#findviewbyid

我的按钮中的一项规则仅在 API 21 或更高版本中使用,我当前的最小值设置为 16(请参见下面的屏幕截图)。我遵循 Android Studio 的建议并覆盖版本,从而创建了 2 个单独的activity_main.xml. 这就是触发可为空行为的原因。

API 限制创建多个 xml 文件

当我删除 v21 xml 文件时,可空值消失了。

现在,在我向阅读本文的人提出的后续问题中,为什么断言不可为空仍然会导致应用程序崩溃?处理多个activity_main.xml版本的正确方法是什么?