首页 > 解决方案 > 内部布局包括适配器内部始终为空

问题描述

我正在回收站视图中显示项目列表。item_layout.xml 有一个包含,它的父级是一个 ContraintLayout,它有一些元素,我可以通过 view.elementID 轻松指向它们

<androidx.constraintlayout.widget.ConstraintLayout...>

...

      <include
        android:id="@+id/incNumberPicker"
        layout="@layout/number_picker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_big"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/lblFamily"
        app:layout_constraintWidth_percent="0.5" />


...
</androidx.constraintlayout.widget.ConstraintLayout>

我可以获取包含布局的参考,但不能获取此包含的父布局。我试过...

    private val incNumberPicker =  view.findViewById<ConstraintLayout>(R.id.incNumberPicker) (not null)
    private val bgNumberPicker = view.bgNumberPicker (null)
    private val bgNumberPicker =  incNumberPicker.findViewById<ConstraintLayout>(R.id.bgNumberPicker) (null)
    private val bgNumberPicker = view.findViewById<ConstraintLayout>(R.id.bgNumberPicker)(null)

这是我的包含布局

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bgNumberPicker"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:focusableInTouchMode="true"
    android:layout_height="wrap_content"
    android:background="@drawable/number_picker_bg">


    <EditText
        android:id="@+id/etNumber"
        style="@style/et_number_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/padding_normal"
        android:layout_marginEnd="@dimen/padding_normal"
        android:backgroundTint="@android:color/transparent"
        android:inputType="number"
        android:maxLength="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnAdd"
        app:layout_constraintStart_toEndOf="@+id/btnRemove"
        app:layout_constraintTop_toTopOf="parent" />

这个父约束有一个背景,我想根据状态改变颜色,所以我想启用/禁用这个约束布局。问题是我可以用普通的 view.etNumber 很容易地指向其余元素,但不能指向父约束。

更新跟踪错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: dev.game.jupiter, PID: 29108
java.lang.IllegalStateException: bgNumberPicker must not be null
    at es.game.jupiter.ui.inventory.productsCounter.InventoriedProductAdapter$InventoriedProductViewHolder.setLocationPickerEditable(InventoriedProductAdapter.kt:95)
    at es.game.jupiter.ui.inventory.productsCounter.InventoriedProductAdapter$InventoriedProductViewHolder.hideShowOptions(InventoriedProductAdapter.kt:85)
    at es.game.jupiter.ui.inventory.productsCounter.InventoriedProductAdapter$InventoriedProductViewHolder$3.onClick(InventoriedProductAdapter.kt:65)
    at android.view.View.performClick(View.java:5205)

谢谢你的帮助。

标签: android

解决方案


推荐阅读