Call getLayoutInflater() in places not in activity
You can use this outside activities - all you need is to provide a Context
:
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
Then to retrieve your different widgets, you inflate a layout:
View view = inflater.inflate( R.layout.myNewInflatedLayout, null );
Button myButton = (Button) view.findViewById( R.id.myButton );
EDIT as of July 2014
Davide's answer on how to get the LayoutInflater
is actually more correct than mine (which is still valid though).
Can't use getLayoutInflater() in Android
Change the following line,
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
to
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
This is because, Service
is a Context
. Service extends ContextWrapper
which extends Context
. You can also use 'this' keyword in the service.
getLayoutInflater cannot resolve
Try like this
LayoutInflater inflater= getActivity().getLayoutInflater();
or
LayoutInflater inflater= youContext.getLayoutInflater();
because method getLayoutInflater()
belong to Activity
and you must get it with getActivity()
method or pass context into your adapter
constructor for example :
public class YourAdapter extends ArrayAdapter {
private Context mContext;
private ArrayList<Strings> someList;
public YourAdapter (Context context, ArrayList<String> someList) {
super(context, R.layout.comment_item, someList);
**this.mContext = context;**
this.someList = someList;
@Override
public View getView(final int position, View convertView, ViewGroup parent) View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = **mContext**.getLayoutInflater();
rowView = inflater.inflate(R.layout.yourItem, null);
rowView.setTag(viewHolder);
}
return rowView;
}
}
and in your Activity
YourAdapter mAdapter = new YourAdapter ( **getActivity()**,someList);
Why isn't the 'getLayoutInflater()' resolving
getLayoutInflater()
can only be used directly inside components who has their own context
like Acitivty
, Application
etc but Fragment
s don't have their own context
Solution:
Declare inflater
outside viewHolder
and initialize it inside constructor and use inflater
instead of getLayoutInflater()
.
LayoutInflater inflater;
public CartAdapter(List<Order> listData, Context context) {
this.listData = listData;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.cart_layout,parent,false);
return new CartViewHolder(itemView);
}
Now use
// remove it , redundant
// LayoutInflater inflater2 = this.getLayoutInflater();
View vi = inflater.inflate(R.layout.content_list_drink_detail,null);
Update
: you are inflating a new view vi
who's TextView
toppingName
and toppingName
will have no data in it , so it's empty
You might want to fetch data from list
like listData.get(position).getQuantity()
// this below code is not needed at all
//LayoutInflater inflater2 = this.getLayoutInflater();
//View vi = inflater2.inflate(R.layout.content_list_drink_detail, null);
//TextView toppingName = (TextView)vi.findViewById(R.id.toppingSelected);
//TextView toppingPrice = (TextView)vi.findViewById(R.id.toppingSelectedPrice);
Null Pointer exception when trying to call getLayoutInflater
I'm not sure why the LayoutInflater is coming back null
but another way to get the LayoutInflater is to simply call LayoutInflater.from(this)
or, if this
isn't a Context, some other Context, maybe by using the method getContext()
or getApplicationContext()
depending on the class you're in.
Fragment getLayoutInflater() vs. LayoutInflater.from(getContext())
Assuming you're using Fragment 1.2.3
(or Fragment 1.3.0-alpha02
, which contains the same fix for a StackoverflowError
when using layoutInflater
in onCreateDialog()
), you should always use layoutInflater
. The fragment's layoutInflater
is the only one that can correctly inflate <fragment>
tags or its replacement, FragmentContainerView
and connect those child fragments to your DialogFragment
correctly.
However, if you're not using child fragments in your XML, you shouldn't see any difference between the two.
Related Topics
Android: Get Facebook Friends List
How to Use an Android Handler to Update a Textview in the UI Thread
Youtube Video Not Playing in Webview
Android - Ondatechangedlistener - How to Set This
How to Programmatically Uninstall a Package in Android
Android: Detect Softkeyboard Open
Wrong Fragment in Viewpager Receives Oncontextitemselected Call
Android Wear App Not Installing Through Handset
Using Onclick Attribute in Layout Xml Causes a Nosuchmethodexception in Android Dialogs
How to Add "Android Design Support Library" to Eclipse with Adt-Plugin
Android.Intent.Action.Screen_On Doesn't Work as a Receiver Intent Filter
See Android Recent Task Executed by the User
The Specified Child Already Has a Parent. You Must Call Removeview() on the Child's Parent First
Getlaunchintentforpackage Is Null for Some Apps
Listview Items Are Not Clickable. Why