I have created a custom layout for ListFragment, using ArrayAdapter, but when the array is empty, no empty message is displayed. I get no errors on the console. My min SDK version is 15, there should be no need to use support libraries.
headline_list.xml (i.e. my custom layout file)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#id/android:list" />
<TextView android:id="#+id/my_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
HeadlineFragment.java
public class HeadlineFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.headline_list, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> headlines = ...; // populate headlines list
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.headline_list, R.id.my_text, headlines));
}
}
You are getting suggestions here, you should do the tries.. Anyway this layout is working perfectly for me:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, from);
setListAdapter(adapter);
return rootView;
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmet"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Here is the view to show if the list is emtpy -->
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
</RelativeLayout>
I had simply forgot to set the text!
After setting it, my TextView is displayed correctly:
<TextView android:id="#id/android:empty"
android:textSize="16sp"
android:textStyle="bold"
android:text="#string/empty_text"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
with string resource in strings.xml file:
<string name="empty_text">There are no data</string>
Unfortunately, the TextView is also displayed in each row of the list, but this is another question.
It may be related to the linear layout and the fact you didn't specified weight attribute to each of the items, so one may overlay another. Check your layout params and this:
Showing empty view when ListView is empty
Related
I have a viewPager that include 3 different fragments with associated layouts (fragment_new_quiz, fragment_completed_quiz, fragment_created_quiz. Each one implements the onCreateView similarly like:
public class NewQuizFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_new_quiz, container, false);
mListView = (ListView) getActivity().findViewById(R.id.list);
mArrayAdapter = new QuizAdapter(mItemList, getActivity());
mArrayAdapter.setNotifyOnChange(true);
setListAdapter(mArrayAdapter);
return rootView;
}
}
The layouts should all be different so I cannot reuse them. They are defined similarly to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fa6a6a"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/createButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create" />
<TextView
android:id="#+id/intro_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Created Quiz"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="299dp" >
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No items."
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
</LinearLayout>
The problem I face is with this code configuration, eclipse gives me the error:
list cannot be resolved or is not a field
related to this line
mListView = (ListView) getActivity().findViewById(R.id.list);
If however I change any of the 3 listview ids to something else in any of the 3 fragments, I don't get the compile time error. However, when I run the code, I get the following error:
11-16 16:32:52.533: E/AndroidRuntime(20935):
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'.
I am kind of clueless about what to do. Is my usage of the various components not appropriate?
In your XML, change these
android:id="#+id/android:list"
android:id="#+id/android:empty"
to these:
android:id="#android:id/list"
android:id="#android:id/empty"
In your Fragments, change
mListView = (ListView) getActivity().findViewById(R.id.list);
to
mListView = (ListView) rootView.findViewById(android.R.id.list);
These are the proper ways to reference resource ids declared in the android namespace.
I am trying to create a ListView inside a ListFragment. My list is customize, so it has been created with an adapter. But the aplication crass and give me this error your content must have a listview whose id attribute is 'android.r.id.list
This is the ListFragment class:
public static class DummySectionFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
Vector<String> names=new Vector<String>();
names.add("Alex");
names.add("Julia");
setListAdapter(new MyAdapter(getActivity(), names));
return rootView;
}
}
This is MyAdapter class:
public class MiAdaptador extends BaseAdapter {
private final Activity activ;
private final Vector<String> list;
public MiAdaptador(Activity activ, Vector<String> list) {
super();
this.actividad = activ;
this.list = list;
}
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = activ.getLayoutInflater();
View view = inflater.inflate(R.layout.adapter_view, null, true);
TextView textView =(TextView)view.findViewById(R.id.titulo);
textView.setText(lista.elementAt(position));
return view;
}
}
My fragment_section_dummy layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name list"
android:gravity="center"
android:layout_margin="10px"
android:textSize="10pt"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
And my adapter_view layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<TextView android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:singleLine="true"
android:text="title"
android:gravity="center"/>
<TextView android:id="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Other text"
android:layout_below="#id/title"
android:layout_alignParentBottom="true"
android:gravity="center"/>
</RelativeLayout>
How could I solve this problem?
I'm starting to think that you should attach the view your inflating to the root view.
Change the inflation to:
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, true);
Another option could be to only use the onCreateView() method to inflate the main layout. And use the onStart() method to set the list adapter. This way makes sure Android knows about your custom layout.
Posting a full stacktrace could help solving your problem.
There was others class who extends to ListFrament (like DummySectionFragment) but these didn`t inflate a layout which have a ListView. So I had to change the parent of there class (Fragment instead of ListFragment) and it has solved.
Thanks for your answer
I know that there has been simialr questions but I can't make it work even though I look at those. The error message is:
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'
. I have tried to change the id to list. In another. This what the code looks like:
public class committeeFragment extends SherlockListFragment {
private CommitteeAdapter adapter;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new CommitteeAdapter(getActivity().getApplicationContext());
setListAdapter(adapter);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.fragment_committee, container, false);
}
public void
refreshList() {
adapter.updateDataSet();
adapter.notifyDataSetChanged();
}
}
It's using the following adapter:
public class CommitteeAdapter extends BaseAdapter {
private
ArrayList<StudentCommittee> committeeList;
private Context context;
public CommitteeAdapter(Context context) {
this.context = context;
CommitteeDatabaseAdapter dbAdapter = new CommitteeDatabaseAdapter(context);
committeeList = dbAdapter.readAllCommittees();
}
public void updateDataSet() {
CommitteeDatabaseAdapter dbAdapter = new CommitteeDatabaseAdapter(context);
committeeList = dbAdapter.readAllCommittees();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
StudentCommittee committee = committeeList.get(position);
View v = vi.inflate(R.layout.list_item_committee, null);
TextView sectionName = (TextView) v.findViewById(R.id.committee_namn);
sectionName.setText(committee.getName());
return v;
}
#Override
public int getCount() {
return committeeList.size();
}
#Override
public StudentCommittee getItem(int position) {
return committeeList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}
Here is the xml-file that specifies the list(fragment_committee):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="#string/tag_fragment_committee"
android:background="#color/white" >
<TextView
style="#style/AppsolutText.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Festerier" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#drawable/divider_sliding_menu_item"/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
style="#style/AppsolutText.Normal"
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Uppdatera för att se en lista över festerier och fadderier. Kräver internetanslutning."/>
</LinearLayout>
Here is the xml for the list item:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/committee_symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<TextView
android:id="#+id/committee_namn"
style="#style/AppsolutText.ListHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
I hope that you can figure out what's wrong. I have another listview that has the same id (list) in another xml-file within the project. I don't know if that's a problem. Please help me solve the problem.
The problem is here :
android:id="#+id/list"
you are adding a new id for your ListView but ListFragment needs a default Android ListView Id
change your ListView Id to :
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Change your ListView in XML to have the following id:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
It's a requirement of using ListFragment/SherlockListFragment
http://developer.android.com/reference/android/app/ListFragment.html
ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "#android:id/list" (or list if it's in code)
You can try to clean your project. If any xml releted error have , you can find it. Then you can change your list id . Find the list id are exist into your gen folder R.java class.
I decided to stop using actionbarsherlock and use the support libraries instead to get action bar in Android 2.1 and above. After doing this, i got the "java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'". What i did to remedy this problem was
to read this http://developer.android.com/reference/android/app/ListFragment.html#q=fragment
then change my fragment layout file from
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"
tools:context=".ItemDetailFragment" />
to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
tools:context=".ItemDetailFragment">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#+id/item_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
... and voila, the RunTimeException with missing android.R.id.list was gone! Of course, i need to edit my new layout and fix it properly, but the key issue here was that i had overridden the fragment layout in its onCreateView method. ActionBarSherlock DID NOT have a problem with this, but with the Android support library, if u do this, you MUST have a ListView in your XML Layout, with the android:id="#id/android:list" as stated in the info linked to above.
Hope this helps (coming from a total newb in Android app dev)!
in my application I use SherlockActionBar with tab navigation.
In the activity MyFragmentActivity that extend SherlockFragmentActivity I add 3 tab with the fragment
mTabsAdapter.addTab(
bar.newTab().setText(getString(R.string.filmtab)),
FragmentFilm.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.cinematab)),
FragmentCinema.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.dintornitab)),
FragmentPdi.class, null);
The FragmentPdi class have this code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_pdi_info, container, false);
return view;
}
and the layout is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/list_places"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
In the MyFragmentActivity I want find the listview for populate it in this way:
final ListView elencoPlaces = (ListView)findViewById(R.id.list_places);
....
but I have NullPointerException!
Why??
In the other fragment (FragmentFilm and FragmentCinema) I have other view like textview without this
problem!!
Please help me!
Thank you.
P.S. Is this the correct way for populate dinamically the views in fragment, or I must populate the view in the fragment class?? If the second how I can pass varible from the activity to the fragment??
if all you have in the layout file is a listview, don't bother overriding oncreateview. the default layout will give you that much. then you can use the methods built into the ListFragment - like getListView() and setListAdapter().
Ok, but I dont undestand why I have another fragment the FragmentCinema where I have this xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:paddingTop="10dip" android:id="#+id/otherCinemas" android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="horizontal" android:id="#+id/plotSeparatorOtherCin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/txtProgrammation">
<ImageView android:layout_gravity="center_vertical" android:background="#drawable/palm_divider_line" android:layout_width="20.0dip" android:layout_height="wrap_content" android:layout_marginRight="5.0dip" />
<TextView android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/altricinema" android:textStyle="bold" />
<ImageView android:id="#+id/showOtherCinemas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_freccia_cinema"
/>
<ImageView android:layout_gravity="center_vertical" android:background="#drawable/palm_divider_line" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#+id/listOtherCinemas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_below="#id/plotSeparatorOtherCin"
android:visibility="gone" >
</ListView>
</RelativeLayout>
and this code of class:
public class FragmentCinema extends SherlockFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_cinema_info, container, false);
return view;
}
}
and when I use the findViewbyId
final ListView cinemasOther = (ListView)findViewById(R.id.listOtherCinemas);
it works! But in FragmentPdi dont work!!
If you want to use ListView in ListFragments, your ListView must have id android:id="#+id/android:list"!
I have a fairly complex layout (containing RelativeLayouts, TextViews and ImageViews) that I want to place above a listview. This view should scroll with the listview.
I tried adding the layout as a header to the listview using this code:
View v = inflater.inflate(R.layout.list_view, container, false);
View header = inflater.inflate(R.layout.header_layout, container, false);
// populate views in the header
mList = (ListView)v.findViewById(R.id.list);
mList.addHeaderView(header);
mAdapter = new ReviewsAdapter(getActivity());
mList.setAdapter(mAdapter); <-- error occurs here
ReviewsAdapter is a custom adapter I've written which extends BaseAdapter.
Upon executing the code, I get this error:
11-25 17:19:14.802: E/AndroidRuntime(1215): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
11-25 17:19:14.802: E/AndroidRuntime(1215): at android.widget.ListView.clearRecycledState(ListView.java:513)
11-25 17:19:14.802: E/AndroidRuntime(1215): at android.widget.ListView.resetList(ListView.java:499)
11-25 17:19:14.802: E/AndroidRuntime(1215): at android.widget.ListView.setAdapter(ListView.java:442)
11-25 17:19:14.802: E/AndroidRuntime(1215): at com.coppi.storefront.product.ProductReviewsFragment.onCreateView(ProductReviewsFragment.java:104)
If I comment out the mList.addHeaderView(header) line I do not get the error. I can also display the header layout without the listview with no problems.
I'm assuming this has something to do with the contents of the header layout but I'm not sure exactly what would be causing it.
Here is the header xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/header_section"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_sides"
android:background="#color/pdp_availability_section_background" >
<TextView
android:id="#+id/header_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_sides"
android:paddingBottom="#dimen/margin_sides"
android:text="#string/ratings_reviews"
android:textColor="#000"
android:textSize="18dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/body_section"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/header_section" >
<TextView
android:id="#+id/product_title"
style="#style/ProductTitleFont"
android:layout_marginBottom="#dimen/product_title_bottom_margin"
android:layout_marginLeft="#dimen/margin_sides"
android:layout_marginRight="#dimen/margin_sides" />
<RelativeLayout
android:id="#+id/attributes_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/product_title"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/margin_sides" >
<LinearLayout
android:id="#+id/overall_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom" >
<TextView
android:id="#+id/overall_label"
style="#style/ProductTitleFont"
android:layout_width="wrap_content"
android:text="#string/overall_rating" />
<ImageView
android:id="#+id/overall_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="#drawable/icon_rating_empty" />
<ImageView
android:id="#+id/overall_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_rating_empty" />
<ImageView
android:id="#+id/overall_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_rating_empty" />
<ImageView
android:id="#+id/overall_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_rating_empty" />
<ImageView
android:id="#+id/overall_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_rating_empty" />
<TextView
android:id="#+id/overall_score"
style="#style/ProductTitleFont"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:text="4.6" />
</LinearLayout>
<Button
android:id="#+id/rate_review_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/overall_section"
android:layout_marginBottom="#dimen/margin_sides"
android:layout_marginTop="#dimen/margin_sides"
android:text="#string/rate_review_button_text" />
</RelativeLayout>
<View
android:id="#+id/attributes_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#id/attributes_section"
android:layout_marginBottom="#dimen/margin_sides"
android:layout_marginTop="#dimen/margin_sides"
android:background="#color/pdp_section_divider" />
<TextView
android:id="#+id/review_count"
style="#style/ProductTitleFont"
android:layout_width="wrap_content"
android:layout_below="#id/attributes_divider"
android:layout_marginLeft="#dimen/margin_sides"
android:text="0 " />
<TextView
style="#style/ProductTitleFont"
android:layout_width="wrap_content"
android:layout_alignBaseline="#id/review_count"
android:layout_marginRight="#dimen/margin_sides"
android:layout_toRightOf="#id/review_count"
android:text="#string/customer_reviews" />
<View
android:id="#+id/review_count_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#id/review_count"
android:layout_marginBottom="#dimen/margin_sides"
android:layout_marginTop="#dimen/margin_sides"
android:background="#color/pdp_section_divider" />
</RelativeLayout>
</RelativeLayout>
Update: I tried reducing the header .xml file to just a single TextView and the problem continues. So I don't believe the problem is being cause by something in the xml.
FrameLayout and AbsListView convert their children layout params to FrameLayout.LayoutParams and AbsListView.LayoutParams. This is where the casting fails.
View header = View.inflate(this, R.layout.header_layout, null);
should fix it.
Edit:
As mentioned in the comments, changing the ViewGroup Parameter of the inflate call also makes it work:
header = inflater.inflate(R.layout.header_layout, null, false);
Although Souvlaki's method will fix the issue and allow you to continue working. It is always best to give the view being inflated a reference to the container it will be in. This allows for Android to correctly inflate it for the right context.
What you should do is find the ListView and then pass that to the inflate.
ListView listView = (ListView) layout.findViewById(R.id.listview);
View header = inflater.inflate(this, R.layout.header_layout, listView, false);
I see same situation.
in my case, it's a trouble that call timing for ListView.addHeaderView().
Before: (Error)
ListView.addHeaderView() is called in Fragment.onCreateView() method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main, container, false);
mListView = (ListView) layout.findViewById(R.id.listview);
mListView.addHeaderView(inflater.inflate(R.layout.list_header_book, listview, false));
return layout;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new BookAdapter(getActivity(), mBookList);
mListView.setOnItemClickListener(mBookItemClickListener);
mListView.setAdapter(mAdapter);
}
After:(OK)
ListView.addHeaderView() is called in Fragment.onActivityCreated() method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new BookAdapter(getActivity(), mBookList);
mListView = (ListView) getView().findViewById(R.id.listview);
mListView.setOnItemClickListener(mBookItemClickListener);
mListView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.list_header_book, null));
mListView.setAdapter(mAdapter);
}
From drspaceboo's post above, this worked for me:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mListLayoutView = inflater.inflate(getLayoutId(), container, false);
int headerLayoutId = R.layout.list_header_layout;
mListHeaderView = inflater.inflate(headerLayoutId, mResourcesListView, false);
// ...
mResourcesListView.addHeaderView(mListHeaderView, null, false);
Where getLayoutId() returns the main layout that contains the ListView xml