android activity - Binary XML file line #11: Error inflating class fragment -
i getting 2 errors in code.
caused by: java.lang.illegalstateexception: fragment com.example.dfoley.write_to_file.topfragment did not create view.
caused by: android.view.inflateexception: binary xml file line #11: error inflating class fragment both pointing line mainactivity.java:21 following setcontentview(r.layout.activity_main);
bottomfragment
package com.example.dfoley.write_to_file; import android.app.listfragment; import android.os.bundle; import android.widget.arrayadapter; import java.util.arraylist; public class bottomfragment extends listfragment { private arrayadapter<stateuser> adapter; @override public void onactivitycreated(bundle saveinstancestate){ arraylist<stateuser> flight = maincontoller.getinstance().getflights(); this.adapter = new arrayadapter<stateuser>(getactivity(), android.r.layout.simple_list_item_1, flight); setlistadapter(this.adapter); super.onactivitycreated(saveinstancestate); } public void refreshlist(){ this.adapter.notifydatasetchanged(); } }
top fragment
package com.example.dfoley.write_to_file; import android.app.activity; import android.app.fragment; import android.content.context; import android.os.bundle; import.android.util.log; import android.view.view; import.android.widget.button; import android.widget.edittext; import java.io.ioexception; import java.io.outputstreamwriter; public class topfragment extends fragment{ private flightsearcher searcher; edittext text1; public interface flightsearcher { public void refreshflightlist(); } @override public void onattach(activity activity) { searcher = (flightsearcher) activity; super.onattach(activity); } @override public void onactivitycreated(bundle savedinstancestate){ setuplisteners(); super.onactivitycreated(savedinstancestate); } public void setuplisteners() { button adduser = (button)getactivity().findviewbyid(r.id.button); adduser.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { writetofile(); searcher.refreshflightlist(); } }); } private void writetofile() { text1=(edittext)getactivity().findviewbyid(r.id.edittext); string addusers = text1.gettext().tostring(); try { outputstreamwriter outputstreamwriter = new outputstreamwriter(getactivity().openfileoutput("userlist", context.mode_private)); outputstreamwriter.write(addusers); outputstreamwriter.close(); } catch (ioexception e) { log.e("exception", "file write failed: " + e.tostring()); } } }
main activity
package com.example.dfoley.write_to_file; import android.app.fragmentmanager; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.view.menu; import android.view.menuitem; public class mainactivity extends fragmentactivity implements topfragment.flightsearcher{ public void refreshflightlist() { fragmentmanager mgr = getfragmentmanager(); bottomfragment bottomfragmentref =(bottomfragment) mgr.findfragmentbyid(r.id.bottom_fragment); bottomfragmentref.refreshlist(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
activiy_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:name="com.example.dfoley.write_to_file.topfragment" android:id="@+id/top_fragment" android:layout_weight="1" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" tools:layout="@layout/topfragment" /> <fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:name="com.example.dfoley.write_to_file.bottomfragment" android:id="@+id/bottom_fragment" android:layout_weight="1" android:layout_below="@+id/top_fragment" android:layout_alignparentleft="true" android:layout_alignparentstart="true" tools:layout="@layout/bottomfragment" />
for both of fragments, not telling how create view. see using tools:layout tag, according tools doc, hint designer; not inflate layout:
"this attribute typically set in tag , used record layout want see rendered @ designtime (at runtime, determined actions of fragment class listed tag)."
thus need override oncreateview, inflate view hierarchy, , return that:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.topfragment, container, false); }
Comments
Post a Comment