Skip to content Skip to sidebar Skip to footer

Illegalstateexception Did Not Create A View When Screen Orientation Changes

I have been trying to make an app that opens a listview fragment (GradeListFragment) from an activity (GradeListActivity). When I click an item, it opens a new fragment (GradeDeta

Solution 1:

Look this doc:

Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts.

So you can not replace the R.id.fragment1 at runtime like:

ft.replace(R.id.fragment1, fragment); //wrong

then the correct way to implement fragment changing at runtime, you can use fragment container FrameLayout to replace Fragment for your 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 you can add/replace fragment in R.id.fragment1.

Hope this help!

Post a Comment for "Illegalstateexception Did Not Create A View When Screen Orientation Changes"