android - ViewGroup.findViewById() 返回 null 因为视图在运行时具有不同的 id
问题描述
- 在
onCreateView()
片段的方法中,我试图用类似于 fab 按钮的行为初始化一个CustomView 。 - 单击此自定义视图后,应该运行一个动画,该动画利用片段
RelativeLayout
中父级中存在的空白。ViewGroup
- 我将它传递
ViewGroup
给构造函数中的CustomView。 - 当我尝试
RelativeLayout
通过调用来创建对空的引用时ViewGroup.findViewById(R.id.target)
,引用 create 持有 null 最终导致NullPointerException
动画 - 但是,如果我
ViewGroup.getChildAt(1)
用来创建参考,动画效果就很好。 - 通过使用观察者,我发现 id 与
ViewGroup.getChildAt(1)
通过 xml 分配给它的 R.id.target 不同。 - 这是什么法术?
- 我已经浏览了几天才能找到描述这种情况的问题。
额外的信息:
- 我们最近从支持库切换到了 androidX。
- 片段.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout>
<!-- Other views -->
</LinearLayout>
<!-- This is the layout i am trying to instantiate -->
<RelativeLayout
android:id="@+id/target"/>
</RelativeLayout>
解决方案
推荐阅读
- c# - 如何将列表中的数字相乘?
- sql - BiqQuery 中的滚动每次点击费用
- shell - `sed` 替换并附加到同一行
- sql-server - 我可以在 SQL 数据库或服务器上设置自定义时间吗?
- android - 我收到此错误:java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView 无法转换为 android.widget.TextView
- wordpress - 使用 WordPress 凭据登录 Flutter
- amazon-web-services - 多个子网的 AWS VPC 费用和成本?
- javascript - 如何使用/引用嵌套的 Promise?
- c# - SELECT * FROM MSISCSIInitiator_TargetClass 给出一般故障错误
- video - 编码后如何在输出文件中保留 DVB 字幕?