Android, findViewById returns null - android

I'm writing some code for Android, the matter is that, when I call findViewById it returns me null, and I cannot understand why! I've been wasting my brain since yesterday, but I cannot figure out the solution. The goal is to set a layout as a header for a listView. Here is my code, respectively my header and my page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<TextView
android:text="#string/bydistance"
android:layout_gravity="left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="13dp"
android:gravity="center_horizontal"/>
<TextView
android:text="#string/byactivity"
android:layout_gravity="right"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="13dp"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
>
<ListView
android:id="#+id/parkList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="5.0dp">
</ListView>
</LinearLayout>
And here my code where I call the addheaderView:
public class NearMeFragment extends Fragment implements View.OnClickListener{
private FragmentManager fragmentManager;
#Override
public void onCreate(Bundle savedBundle){
super.onCreate(savedBundle);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
//TODO remove
make_test(v, container);
return v;
}
public void openTest(View view){
new LiveComment().show(getChildFragmentManager(), "dialog");
}
public void onClick(View view){
if (fragmentManager == null)
fragmentManager = getChildFragmentManager();
//if (view.getId() == R.id.test){
fragmentManager.beginTransaction().add(new LiveComment(), "livecomment").commit();
}
private void make_test(View v, ViewGroup container) {
ListView listView = (ListView) v.findViewById(R.id.parkList);
Park[] list = new Park[3];
list[0] = new Park("parco a", "acquasparta", false, false, 1, 2, 3);
list[1]=new Park("parco b", "perugia", false, false, 1, 2, 3);
list[2]=new Park("parco b", "perugia", false, false, 1, 2, 3);
ParkListAdapter adapter = new ParkListAdapter(v.getContext(), R.layout.small_park_container,list);
LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
}
The error is given at
listView.addHeaderView(header)

R.id.header is inside v not inside container.
Change
LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
with
LinearLayout header = (LinearLayout) v.findViewById(R.id.header);
about the ViewGroup container to doc says:
If non-null, this is the parent view that the fragment's UI should be
attached to. The fragment should not add the view itself, but this can
be used to generate the LayoutParams of the view.
Edit: Since your header view is in another Layout you have to inflate it too:
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
View header = inflater.inflate(R.layout.headerlayout, null);
//TODO remove
make_test(v, header);
return v;
}
and
private void make_test(View v, View header) {
// your code
// you have to remove LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}

Related

Dynamically populate a dialogue inside a fragment

I have created a fragment and want to start a dialogue on a button click on which i would display n numbers of texts distributed vertically using Linear layout vertical orientation. To begin with i have created a textview which i am populating dynamically. But i get a null pointer exception saying the any of the layout i referenced here points to NULL.
View view,tempView;
TextView myName, myPhNo, myEmail, myDob;
ImageButton selectRingTone;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_profile, container, false);
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
myName = (TextView)view.findViewById(R.id.username);
myPhNo = (TextView)view.findViewById(R.id.userPhNo);
myEmail = (TextView)view.findViewById(R.id.useremailId);
myDob = (TextView)view.findViewById(R.id.userdob);
selectRingTone = (ImageButton)view.findViewById(R.id.selectRingTone);
setUserProfileData();
//setUserTextStatus();
//setUserAudioStatus();
selectRingTone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseAndSetRingTone();
}
});
return view;
}
private void chooseAndSetRingTone(){
final Dialog fbDialogue = new Dialog(view.getContext(), android.R.style.Theme_Black);
fbDialogue.getWindow().setTitle("Select your audio status song");
fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0)));
fbDialogue.setContentView(R.layout.fragment_selectsong);
getSongsAndFillDialogue();
fbDialogue.setCancelable(true);
fbDialogue.show();
}
private void getSongsAndFillDialogue(){
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) tempView.findViewById(R.id.eachRingToneSong);
TextView tv = new TextView(tempView.getContext());
tv.setText("Hi hello");
Button b = new Button(tempView.getContext());
b.setText("abcde");
container.addView(tv);
container.addView(b);
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLyt"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollCotainerForRingToneSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/eachRingToneSong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<TextView
android:id="#+id/song1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde"
/>-->
</LinearLayout>
</ScrollView>
</RelativeLayout>
you are creating View for your Dialog in onCreateView method:
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
and fulfilling it using getSongsAndFillDialogue method, but you are setting for your Dialog another new instance passing only resource id:
fbDialogue.setContentView(R.layout.fragment_selectsong);
when you are passing just id then Dialog is inflating this layout by itself (just like you). So there are two layouts created, one in Activity - fulfilled, and one created "automatically" by setContentView method, but not touched at all by your methods (so it stays empty)
instead of passing resource id pass already prepared tempView, like below:
fbDialogue.setContentView(tempView);

Put a button on a page from a ViewPager

I'm using ViewPager. I want to put a button on each page, but in doing so fills the entire page. I would like to determine the width and height of button but do not know how.
This is my code:
Pager Activity
public class PagerActivity extends Activity {
PagerContainer mContainer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (PagerContainer) findViewById(R.id.pager_container);
ViewPager pager = mContainer.getViewPager();
PagerAdapter adapter = new MyPagerAdapter();
pager.setAdapter(adapter);
//Necessary or the pager will only have one extra page to show
// make this at least however many pages you can see
pager.setOffscreenPageLimit(adapter.getCount());
//A little space between pages
pager.setPageMargin(15);
//If hardware acceleration is enabled, you should also remove
// clipping on the pager for its children.
pager.setClipChildren(false);
}
//Nothing special about this adapter, just throwing up colored views for demo
private class MyPagerAdapter extends PagerAdapter {
#Override
public Object instantiateItem(ViewGroup container, int position) {
TextView view = new TextView(PagerActivity.this);
view.setText("Item "+position);
view.setGravity(Gravity.CENTER);
view.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));
view.setTextColor(Color.parseColor("#CA2C68"));
Button buttonView = new Button(PagerActivity.this);
buttonView.setBackgroundResource(R.drawable.button_states_azul);
container.addView(buttonView);
return buttonView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#FF7CB8"
android:id="#+id/linear" >
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#CA2C68"
android:layout_gravity="bottom"/>
</LinearLayout>
<com.example.slidee.PagerContainer
android:id="#+id/pager_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/linear"
android:layout_alignParentBottom="true"
android:layout_below="#+id/linear"
android:background="#29C5FF" >
<android.support.v4.view.ViewPager
android:layout_width="234dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#29C5FF" />
</com.example.slidee.PagerContainer>
ViewPager expects the contained view to occupy the whole container.
Use some layout to contain a button:
LinearLayout lay = new LinearLayout(PagerActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
buttonView.setLayoutParams(lp);
lay.addView(buttonView);
container.addView(lay);
an other way to do it is to make a new xml file and inflate it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewpager_button"
android:background="#drawable/button_states_azul"/>
</LinearLayout>
and in instantiateItem
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View pagerView = inflater.inflate(R.layout.viewpager_layout, null);
Button button = (Button) pagerView.findViewById(R.id.viewpager_button);
container.addView(button);
You are creating the button dynamically and hence by default it does a match_parent for width and height.
Add this line:
buttonView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
This is how i do it, create a xml layout you want to inflate. Add all the view you are intending. Then inside your adapter class, inside instantiateItem override do something like this. remember to adjust your xml views to fit parents width and height as you wish.
#Override
public Object instantiateItem(ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_view, container, false);
// get reference to your button over here if you have one
Button b = (Button) view.findViewById(R.id.enter_button);
container.addView(view);
return view;
}

Render Dialog elements with Material Design Style

I'm creating a dialog fragment with a layout that uses checkboxes. However, I'm never able to render them with the material design look in pre-lollipop devices. However, I'm able to get it done in a regular activity. What do I have to do when dealing with DialogFragments?
These are parts of my DialogFragment code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Dialog);
Window window = dialog.getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.getAttributes().windowAnimations = R.style.Share_Multiplayer_Animation;
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.share_multiplayer_scorecard, container, false);
ButterKnife.bind(this, view);
ViewGroup shareContainer = ButterKnife.findById(view, R.id.container);
List<MultiplayerRound> multiplayerRoundList = mRound.getMultiplayerRoundList();
for (int i = 0; i < multiplayerRoundList.size(); i++) {
MultiplayerRound mpRound = multiplayerRoundList.get(i);
View rowView = inflater.inflate(R.layout.multiplayer_share_row, shareContainer, false);
TextView nameTv = ButterKnife.findById(rowView, R.id.et_name);
nameTv.setText(mpRound.getName());
CheckBox checkBox = ButterKnife.findById(rowView, R.id.cb_send_scorecard);
final EditText emailET = ButterKnife.findById(rowView, R.id.et_email);
emailET.addTextChangedListener(new CheckboxControllerTextWatcher(checkBox));
emailET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
emailET.setSelection(emailET.getText().length());
}
});
String email = getEmail(mpRound);
emailET.setText(email);
updateCheckboxState(email, checkBox);
shareContainer.addView(rowView);
mPlayerViews[i] = rowView;
rowView.setTag(mpRound);
}
return view;
}
This is my dialog layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin">
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/tv_title"
style="#style/MaterialDialog.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/round.multiplayer.share.scorecard"/>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:orientation="vertical"/>
<LinearLayout
style="#style/MaterialDialog.ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<com.devspark.robototextview.widget.RobotoButton
android:id="#+id/btn_dismiss"
style="#style/MaterialDialog.Button"
android:layout_width="wrap_content"
android:text="#string/share_dismiss"/>
<com.devspark.robototextview.widget.RobotoButton
android:id="#+id/btn_send"
style="#style/MaterialDialog.Button"
android:layout_width="wrap_content"
android:text="#string/action_send"/>
</LinearLayout>
</LinearLayout>
Thank you.
As of revision 23 of AppCompat, you can use AppCompatDialogFragment to create a material design DialogFragment that is compatible back to Android 2.1.

Fragment disappears after setAdapter on ListView

I have a ListView in a Fragment containing also some other UI-Elements. Everything works until I call setAdapter on the ListView. In that moment I can debug that the ListView is filled with the elements but immmediately after the whole fragment disappears (including the other UI-Elements). If I set the Adapter 0,1 seconds later, everything works.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_listview, null);
mListView = (ListView)mView.findViewById(R.id.list_items);
TextView emptyView = (TextView)mView.findViewById(R.id.text_empty);
mListView.setEmptyView(emptyView);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// this is the only work around to not make the list view disappear
loadContacts();
}
}, 100);
// if I call loadContacts() here, the Fragment disappears
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person p = mContacts.get(position);
if (pickerInterface != null) {
pickerInterface.pickedContact(p);
}
}
});
return mView;
}
public void loadContacts() {
mContacts = ContactsDataSource.getAllPhoneContacts(getActivity(), true);
mAdapter = new ContactsAdapter(getActivity());
mAdapter.mContacts = mContacts;
mAdapter.isPlainList = true;
// after the following line the Fragment is filled and then immediately disappears
mListView.setAdapter(mAdapter);
}
the ContactsAdapter is very simple:
public class ContactsAdapter extends BaseAdapter {
public List<Person> mContacts = new ArrayList<Person>();
public Activity context;
// Constructor
public ContactsAdapter(Activity c){
context = c;
}
#Override
public int getCount() {
return mContacts.size();
}
public Person getItem(int position){
return mContacts.get(position);
}
#Override
public boolean isEmpty()
{
return mContacts.size() == 0;
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_contact, null);
}
Person person = mContacts.get(position);
TextView tvName = (TextView) convertView.findViewById(R.id.text_name);
tvName.setText(person.getDisplayName());
return convertView;
}
}
I have spent hours debugging and searching for reasons and have no clue. Thanks for any idea.
Edit: XML as following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<ListView
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/line_horizontal"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/text_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:layout_alignParentTop="true"
android:text="#string/label_no_entries"
android:textColor="#color/grey"
android:textSize="21sp" />
<View
android:id="#+id/line_horizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#+id/layout_navigation"
android:background="#color/black" />
<RelativeLayout
android:id="#+id/layout_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:id="#+id/text_scrollback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:drawablePadding="10dp"
android:drawableRight="#drawable/backward"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:text="#string/activity_contacts_backward_label"
android:textColor="#color/mediumgrey" />
<View
android:id="#+id/view_coverback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_scrollback"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/text_scrollback"
android:layout_alignTop="#+id/text_scrollback"
android:background="#c0ffffff" />
<TextView
android:id="#+id/text_scrollforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:drawableLeft="#drawable/forward"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:text="#string/activity_contacts_forward_label"
android:textColor="#color/mediumgrey" />
<View
android:id="#+id/view_coverforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_scrollforward"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/text_scrollforward"
android:layout_alignTop="#+id/text_scrollforward"
android:background="#c0ffffff" />
<TextView
android:id="#+id/text_pages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/text_scrollforward"
android:layout_toLeftOf="#+id/text_scrollforward"
android:layout_toRightOf="#+id/text_scrollback"
android:gravity="center"
android:textColor="#color/mediumgrey"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
Where did you initialize your "mListView"?
The time that fragment inflated is "onCreateView()" and general way of getting view instances from layout is done on this method. like below
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, null);
mListView = (ListView)v.findViewById(R.id.list_items);
...
return v;
}
How about trying to initalize your views(ListViews, and others) in "onCreateView"?
I think that the problem is caused from getting view instances before fragment is inflated.
Not sure what your problem is. May be with the custom super class. Otherwise, wild guess here, but try inflating your view like this.
mView = inflater.inflate(R.layout.fragment_listview, container, false);
First of all, storing a context object can be dangerous and should therefore be avoided if possible... You can easily get rid of it in your adapter with following steps (that hopefully also solves your problem):
--> try following constructor for your adapter:
public ContactsAdapter(Context c){
super(c, R.layout.yourlayout_listitem_name;
}
--> and adjust the getView-method to use something like:
final View rowView = View.inflate(viewGroup.getContext(), R.layout.yourlayout_listitem_name, null);
instead of the inflater-if-block. then always return rowView at the end.
and just to be sure I would remove the overriden-method:
#Override
public long getItemId(int position) {
return position;
}
since it is not doing anything useful anyway ;-)
Thank you for all your support. By removing all code and re-assembling step by step I foundt the reason. Just in case someone runs in the same problem one day.
I had an OnScrollListener on the ListView declared in the superclass. In the OnScrollListener I was hiding the navigation Arrows on demand with Visibility.GONE. Even if the NavigationArrows did not affect the layout of the ListView this was the problem. By setting Visibility.INVISIBLE everything works fine. I guess this is related to some re-drawing-issue of the whole layout that does not work.

ListView addHeaderView glitching around after onResuming Activity

In my ListFragment I want a headerView to make the app look like its website counterpart.
onCreateView():
View headerView = inflater.inflate(R.layout.header, null, false);
onStart():
if(headerView != null) {
getListView().addHeaderView(headerView);
}
After pausing and resuming the activity, the headerView has a large top margin is glitching around: https://www.youtube.com/watch?v=Q_zhr8w5Yzg
header.xml
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/news_bubble"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:text="#string/str_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
Edit: This question is solved. If anybody stumbles upon this error, this is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
[...]
return rootView;
}
You need to make the list view as the container for your header view. So inflate the listView first and then inflate the header view as follows
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout,container,false);
ListView listView = (ListView)view.findViewById(R.id.my_list);
View headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
return view;
}

Categories

Resources