Here is the info about the exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.v.layout, PID: 13198
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #0: Duplicate id 0x7f070029, tag null, or parent id 0xffffffff with another fragment for app.v.layout.contentFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3680)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at app.v.layout.MainActivity.onConfigurationChanged(MainActivity.java:21)
at android.app.ActivityThread.performActivityConfigurationChanged(ActivityThread.java:4942)
at android.app.ActivityThread.performConfigurationChangedForActivity(ActivityThread.java:4809)
at android.app.ActivityThread.performConfigurationChangedForActivity(ActivityThread.java:4787)
at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:5134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1728)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
The following is the code for MainActivity:
package app.v.layout;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.activity_main_land);
}else{
setContentView(R.layout.activity_main);
}
}
}
And two xmls:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"
tools:context="app.v.layout.MainActivity">
<fragment
class="app.v.layout.contentFragment"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</fragment>
<fragment
class="app.v.layout.InteractionFragment"
android:id="@+id/interaction"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</fragment>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/colorPrimary"
tools:context="app.v.layout.MainActivity">
<fragment
android:id="@+id/content2"
class="app.v.layout.contentFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/interaction2"
class="app.v.layout.InteractionFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
</LinearLayout>
When I ratate the screen, the first time it works well, but then it will just stop. I wonder why. And is there a way to fix this? Thanks in advance.
In fact, there isn't much difference between the two xmls that I want to switch. If only I can adjust the attributes of the fragment, say, the height or width, then, I will not need to use the fuction: setContentView.
Ps: I've already add the following line: android:configChanges="orientation|screenSize", however, as I put it earlier, it will only work one time before it stop abruptly. the AndroidManifest.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.v.layout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>