Android, findViewById returns null -
i'm writing code android, matter that, when call findviewbyid returns me null, , cannot understand why! i've been wasting brain since yesterday, cannot figure out solution. goal set layout header listview. here code, respectively header , 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 code call 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 given @
listview.addheaderview(header)
r.id.header
inside v
not inside container
.
change
linearlayout header = (linearlayout) container.findviewbyid(r.id.header);
with
linearlayout header = (linearlayout) v.findviewbyid(r.id.header);
about viewgroup container doc says:
if non-null, parent view fragment's ui should attached to. fragment should not add view itself, can used generate layoutparams of view.
edit: since header view in layout have inflate 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) { // code // have remove linearlayout header = (linearlayout) container.findviewbyid(r.id.header); listview.addheaderview(header); listview.setadapter(adapter); }
Comments
Post a Comment