Reputation: 33511
I have this code, where I define my own AlertDialog.Builder
:
public class UnlockDialog extends AlertDialog.Builder {
public UnlockDialog(Activity context) {
super(context);
LayoutInflater inflater = context.getLayoutInflater();
View dlgView = inflater.inflate(R.layout.unlock_dialog, null);
setView(dlgView);
}
}
the code works fine, but I get a warning on the inflater.inflate
call:
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
I had this issue as well in my adapters, where I could resolve it using providing a parent
and false
, as I found here: Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)
However, in the case above, I don't seem to have a parent
available. I tried context.getParent()
, but that didn't work.
Upvotes: 2
Views: 2066
Reputation: 55340
The warning should not apply in this case. The root
parameter, when attachToRoot
is false, is only used to call its generateDefaultLayoutParams()
method and assign the resulting LayoutParams
to the inflated view.
In this case, the dialog will overwrite them, so they would be unused anyway.
Please check this article, particularly the section Every Rule Has An Exception.
However, because the result will go into the dialog, which does not expose its root view (in fact, it doesn’t exist yet), we do not have access to the eventual parent of the layout, so we cannot use it for inflation. It turns out, this is irrelevant, because AlertDialog will erase any LayoutParams on the layout anyway and replace them with match_parent.
Upvotes: 1