How to manage orientation in landscape and portrait using fragment
I create one example in Android 3.0 and I use the fragment in it. when I change the mode landscape to portrait or viseversa it give me the error as following but when I comment the fragment calling part it works smoothly as per my changes. for this I create layout-land folder and put may xml file with changes.
does any one have a hint or solution or example for match with this?
this is my fragment:
<fragment
class="com.Organisemee.fragment.TaskListFragment"
android:id="@+id/tasklistfrag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And this is error:
07-01 12:38:33.363: ERROR/AndroidRuntime(641): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Organisemee/com.Organisemee.OrganisemeeList}: android.view.InflateException: Binary XML file line #167: Error inflating class fragment
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3096)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread.access$1600(ActivityThread.java:123)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:997)
07-01 12:38:33.36开发者_C百科3: ERROR/AndroidRuntime(641): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.os.Looper.loop(Looper.java:126)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread.main(ActivityThread.java:3997)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at java.lang.reflect.Method.invoke(Method.java:491)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at dalvik.system.NativeStart.main(Native Method)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): Caused by: android.view.InflateException: Binary XML file line #167: Error inflating class fragment
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.Activity.setContentView(Activity.java:1777)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at com.Organisemee.OrganisemeeList.onCreate(OrganisemeeList.java:73)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): ... 12 more
07-01 12:38:33.363: ERROR/AndroidRuntime(641): Caused by: java.lang.IllegalStateException: Fragment com.Organisemee.fragment.TaskListFragment did not create a view.
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.app.Activity.onCreateView(Activity.java:4114)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
07-01 12:38:33.363: ERROR/AndroidRuntime(641): ... 22 more
Need to define a Fragment in XML Like This.
<RelativeLayout
android:id="@+id/main_tasklist_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/main_viewmenu_layout"
android:layout_below="@+id/main_tasklist_outer">
<fragment
class="com.Organisemee.fragment.TaskListFragment"
android:id="@+id/tasklistfrag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
To Call the Fragment put this code in java file
Fragment f = listFragment();
FragmentTransaction ListFragft = getFragmentManager().beginTransaction();
ListFragft.replace(R.id.main_tasklist_layout, f);
ListFragft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ListFragft.addToBackStack(null);
ListFragft.commit();
This Works fine in change both orientation, landscape to portrait and vice versa.
精彩评论