Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)
Instead of doing
convertView = infalInflater.inflate(R.layout.list_item, null);
do
convertView = infalInflater.inflate(R.layout.list_item, parent, false);
It will inflate it with the given parent, but won't attach it to the parent.
Avoid passing null as the view root (when inflating custom layout in AlertDialog)
Do it like this:
View content = inflater.inflate(R.layout.dialog_customd, parent, false);
Avoid passing null as the view root warning when inflating view for use by AlertDialog
Use this code to inflate the dialog view without a warning:
View.inflate(context, R.layout.dialog_edit, null);
No parent found for avoid passing null as the view root
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.
Avoid passing null as the view root with popupwindow
You could just use:
context = popupView.getContext();
inflater.inflate(R.layout.resetpop, new LinearLayout(context), false);
That should solve the warning.
What should I pass for root when inflating a layout to use for a MenuItem's ActionView?
I would simply do it like this:
menuItem.setActionView(R.layout.action_view_layout);
Let Android inflate the view for you.
If you need to do some extra changes on this ImageView call
ImageView imageView = (ImageView) menuItem.getActionView();
Update
In order to cater to your curiosity. That is what folks from Google do under the hood:
public MenuItem setActionView(int resId) {
final Context context = mMenu.getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
setActionView(inflater.inflate(resId, new LinearLayout(context), false));
return this;
}
Related Topics
Android Edittext Onchange Listener
Creating a PDF File in Android Programmatically and Writing in It
Android: Unable to Instantiate Activity/Classnotfoundexception
Android Update Textview in Thread and Runnable
How to Implement a Contentobserver for Call Logs
How to Enable Standard Copy Paste for a Textview in Android
Adding Local .Aar Files to My Gradle Build
Close Application and Remove from Recent Apps/
Android: Overlay on Android Camera Preview
Setresult Does Not Work When Back Button Pressed
Android Studio - Creating Modules Without Copying Files
How to Change Bitmap Image Color in Android
Google Glass Gdk: How to Communicate with Android Device
The Process of the Service Is Killed After the Application Is Removed from the Application Tray
How to Update My Adt in Eclipse
How to Check Whether the Sim Card Is Available in an Android Device
Flutter Does Not Find Android Sdk
How to Add the Aidl File to Android Studio (From the In-App Billing Example)