android - illegalstateexception did not create a view when screen orientation changes -
i have been trying make app opens listview fragment (gradelistfragment) activity (gradelistactivity). when click item, opens new fragment (gradedetailfragment). if in portrait mode, replaces list fragment in fragment1. if in landscape, replaces empty fragment2. problem app crashes when switch orientation. switching both perspectives gets same error.
04-09 12:10:52.240: e/androidruntime(2221): fatal exception: main 04-09 12:10:52.240: e/androidruntime(2221): process: bcs421.christophergoepfert.hwk.gradeapp.presentation, pid: 2221 04-09 12:10:52.240: e/androidruntime(2221): java.lang.runtimeexception: unable start activity componentinfo{bcs421.christophergoepfert.hwk.gradeapp.presentation/bcs421.christ ophergoepfert.hwk.gradeapp.presentation.gradelistactivity}: android.view.inflateexception: binary xml file line #8: error inflating class fragment 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3912) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread.access$900(activitythread.java:144) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread$h.handlemessage(activitythread.java:1284) 04-09 12:10:52.240: e/androidruntime(2221): @ android.os.handler.dispatchmessage(handler.java:102) 04-09 12:10:52.240: e/androidruntime(2221): @ android.os.looper.loop(looper.java:135) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread.main(activitythread.java:5221) 04-09 12:10:52.240: e/androidruntime(2221): @ java.lang.reflect.method.invoke(native method) 04-09 12:10:52.240: e/androidruntime(2221): @ java.lang.reflect.method.invoke(method.java:372) 04-09 12:10:52.240: e/androidruntime(2221): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 04-09 12:10:52.240: e/androidruntime(2221): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 04-09 12:10:52.240: e/androidruntime(2221): caused by: android.view.inflateexception: binary xml file line #8: error inflating class fragment 04-09 12:10:52.240: e/androidruntime(2221): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:763) 04-09 12:10:52.240: e/androidruntime(2221): @ android.view.layoutinflater.rinflate(layoutinflater.java:806) 04-09 12:10:52.240: e/androidruntime(2221): @ android.view.layoutinflater.inflate(layoutinflater.java:504) 04-09 12:10:52.240: e/androidruntime(2221): @ android.view.layoutinflater.inflate(layoutinflater.java:414) 04-09 12:10:52.240: e/androidruntime(2221): @ android.view.layoutinflater.inflate(layoutinflater.java:365) 04-09 12:10:52.240: e/androidruntime(2221): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:377) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activity.setcontentview(activity.java:2144) 04-09 12:10:52.240: e/androidruntime(2221): @ bcs421.christophergoepfert.hwk.gradeapp.presentation.gradelistactivity.oncreate(gradelistactivity.java:29) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activity.performcreate(activity.java:5933) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) 04-09 12:10:52.240: e/androidruntime(2221): ... 11 more 04-09 12:10:52.240: e/androidruntime(2221): caused by: java.lang.illegalstateexception: fragment android.app.listfragment did not create view. 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.fragmentmanagerimpl.oncreateview(fragmentmanager.java:2145) 04-09 12:10:52.240: e/androidruntime(2221): @ android.app.activity.oncreateview(activity.java:5282) 04-09 12:10:52.240: e/androidruntime(2221): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:733)
mainactivity not crash when restarts.
public class mainactivity extends activity{ public gradecollection collection; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //grade data } } public void startgradelistactivity(view v){ intent = new intent(mainactivity.this, gradelistactivity.class); //putextra gradecollection startactivity(i); } }
the main_activity xml
<textview android:id="@+id/textviewname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <ratingbar android:id="@+id/ratingbar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numstars="4" android:stepsize="1" /> <textview android:id="@+id/textviewgradenumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/gradenumber" /> <textview android:id="@+id/textviewgradeletter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" /> <button android:id="@+id/buttonviewgrades" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onclick="startgradelistactivity" android:text="@string/viewgrades" />
gradelistactivity crashes @ ft.commit();, maybe problem i'm importing android.support.v4.app.fragmentactivity;?
public class gradelistactivity extends fragmentactivity { //private gradecollection listgrades; //private arraylist<string> listcatagory; //private arrayadapter<string> listadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.catagory_list); listfragment fragment = new gradelistfragment(); fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment1, fragment); ft.commit(); } }
this gradelistfragment.
public class gradelistfragment extends listfragment{ @override public view oncreateview( layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.oncreate(savedinstancestate); view v = inflater.inflate(r.layout.fragment_category, container, false); //data handling return v; } }
here xml catagory_list portrait
<fragment android:id="@+id/fragment1" android:name="android.app.listfragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.03" />
and here landscape
<fragment android:id="@+id/fragment1" android:name="android.app.listfragment" android:layout_width="271dp" android:layout_height="match_parent" /> <framelayout android:id="@+id/fragment2" android:layout_width="match_parent" android:layout_height="match_parent" />
i have omitted actual data being passed gradedetailfragment because know aren't problem. have seen this page not understand how works can't test it. have tried onconfigchange in manifest can't change appropriate layout. if solve issue or explain link, grateful.
look doc:
note: when add fragment activity layout defining fragment in layout xml file, cannot remove fragment @ runtime. if plan swap fragments in , out during user interaction, must add fragment activity when activity first starts.
so can not replace r.id.fragment1
@ runtime like:
ft.replace(r.id.fragment1, fragment); //wrong
then correct way implement fragment changing @ runtime, can use fragment container framelayout
replace fragment
r.id.fragment1
as:
<framelayout android:id="@+id/fragment1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.03" />
then can add/replace fragment in r.id.fragment1
.
hope help!