【问题标题】:How do I handle missing views in different layout configurations when using view binding?使用视图绑定时,如何处理不同布局配置中缺少的视图?
【发布时间】:2021-04-04 00:36:27
【问题描述】:

我了解任何具有 ID 的 XML 元素都应通过视图绑定自动拉入活动类。但是,Android Studio 一直提示我的按钮可以为空,并且需要 ?.!!. 才能编译。

如果我用!!. 断言不可为空,它实际上会导致按钮的运行时 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
    }
    
}

此处应用的 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>

我在视图绑定文档中找到了罪魁祸首:

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

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

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

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

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

提前致谢!

【问题讨论】:

  • 文档说,当视图存在于布局的某些配置中,但不存在于其他配置中时,布局的绑定对象将视图引用定义为@Nullable,因为它可能存在也可能不存在,取决于您要绑定的文件的版本。所以btnSelectDatenullable,即它的类型是Button?而不是Button,所以你需要在尝试设置点击监听器之前对它进行null检查。这很重要,因为如果您在 btnSelectDate 将为空,并且它会崩溃。这就是它警告你的原因

标签: android kotlin android-button android-viewbinding


【解决方案1】:

我在视图绑定文档中找到了罪魁祸首:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多