Reputation: 9234
Trying to change fragment width when user rotate device to landscape mode
That is my fragment:
<fragment
android:id = "@+id/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="myPackage.MenuFragment" />
And that is my onConfigurationChanged method:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
findViewById(R.id.menu).getLayoutParams().width = (int) (getWindowManager()
.getDefaultDisplay().getWidth());
}
But when I rotate device the output is crash:
05-14 03:00:51.379: E/AndroidRuntime(3117): FATAL EXCEPTION: main 05-14 03:00:51.379: E/AndroidRuntime(3117): android.view.InflateException: Binary XML file line #13: Error inflating class fragment 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 05-14 03:00:51.379: E/AndroidRuntime(3117): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.app.Activity.setContentView(Activity.java:1654) 05-14 03:00:51.379: E/AndroidRuntime(3117): at softserveinc.dbyst.reveal.Reveal_prototypeActivity.onConfigurationChanged(Reveal_prototypeActivity.java:71) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:4153) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:4246) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2215) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.os.Handler.dispatchMessage(Handler.java:99) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.os.Looper.loop(Looper.java:143) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.app.ActivityThread.main(ActivityThread.java:4914) 05-14 03:00:51.379: E/AndroidRuntime(3117): at java.lang.reflect.Method.invokeNative(Native Method) 05-14 03:00:51.379: E/AndroidRuntime(3117): at java.lang.reflect.Method.invoke(Method.java:521) 05-14 03:00:51.379: E/AndroidRuntime(3117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-14 03:00:51.379: E/AndroidRuntime(3117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-14 03:00:51.379: E/AndroidRuntime(3117): at dalvik.system.NativeStart.main(Native Method) 05-14 03:00:51.379: E/AndroidRuntime(3117): Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f050001, tag null, or parent id 0x0 with another fragment for myPackage.MenuFragment 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:275) 05-14 03:00:51.379: E/AndroidRuntime(3117): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558) 05-14 03:00:51.379: E/AndroidRuntime(3117): ... 19 more
What i'v done wrong ?
Upvotes: 2
Views: 3206
Reputation: 28162
Yes your setcontentview crashes because you got the fragment defined in XML. You got a couple of options here (that I know of, I'm not a pro on this issue ;) ). You can put a FrameLayout where the fragment is and then add the fragment with a replace transaction:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout_container, yourFragmentObject);
fragmentTransaction.commit();
Alternatively you can test if setRetainInstance(true) will work.
Upvotes: 1