android - 膨胀后 subview 为空

标签 android android-layout

好吧,我这样做(不是实际代码)

try {
    final View view = LayoutInflater.from(context).inflate(R.layout.ownComponent, null);
} catch (InflateExpection e) {
}

if (view != null) {
    // This child is null about 5/10 times!

    View child = view.findViewById(R.id.ownComponentChild);
}

我读到在膨胀之后不能保证 subview 被膨胀,那么当所有 child 都准备好时获得回调的巧妙方法是什么?

最佳答案

也许我误解了你想做的事情,但看起来你正在膨胀一个 View 而不是一个布局......

尝试

View view = LayoutInflater.from(context).inflate(R.layout.LAYOUT_THAT_HOLDS_ownComponent, this, true);

然后 View 将包含整个布局,您可以从中通过 Id 找到 child

view.findViewById(...);

编辑:

很难知道它是否相关,因为您没有发布足够的代码,但试试这个: 从 try/catch 中获取 View view 并将其作为类成员。松开决赛并投下 child 。

示例(假设 ownComponentChild 是一个 FrameLayout):

FrameLayout child = (FrameLayout)view.findViewById(R.id.ownComponentChild);

关于android - 膨胀后 subview 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10514637/

相关文章:

java - Android - 将 MediaMuxer 与 MediaExtractor 和 PCM 流一起使用会导致视频帧损坏

android - 如何隐藏 Activity ?

java - 尝试在Android平台上的两个类之间传递int

android - 格局反了?

java - NullPointerException:LinearLayout 为 Null

android - 如何通过半透明背景停止点击事件?

java - 日期数组中最长的连续子序列

java - 中继的蓝牙连接问题 - RxAndroidBle

android - 如何最大化 Android 小部件尺寸(宽度)?

android - 在android中开发对角线布局