Niko
Niko

Reputation: 8153

Children views are null after inflate

Okay so I do this (not actual code)

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);
}

I read that after inflate it is not guaranteed that child views are inflated, so what would be neat way to get callback when all childs are ready?

Upvotes: 2

Views: 1292

Answers (2)

Niko
Niko

Reputation: 8153

This seems to happen randomly so my only guess is that memory is getting low in this case, because I have to recreate so many UI components fast to get this reproduced.

Upvotes: 0

Lior Iluz
Lior Iluz

Reputation: 26563

Maybe I misunderstood what you're trying to do, but it seems like you're inflating a View and not a layout...

Try

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

and then view will hold the entire layout, from which you can find the child by Id with

view.findViewById(...);

Edit:

Hard to know if it's related as you didn't post enough code but try this: Get the View view out of the try/catch and put it as a class member. loose the final and cast the child.

example (assuming ownComponentChild is a FrameLayout):

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

Upvotes: 1

Related Questions