2

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>

4 Answers4

0

update your manifest file with this and try,

<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>

Android will manage itself to avoid recreation.

Jithu S
  • 76
  • 3
0

You need to listen the configuration changes of screen rotation, that you've already done. But you need to explicitly declare that into your manifest in the specific activity with

android:configChanges="orientation|screenSize"
noman404
  • 928
  • 1
  • 8
  • 23
  • thanks, I already declared it. Still, the exceptions wouldn't let me go. – Shun Vedeus Mar 23 '18 at 11:14
  • the causes is mentioned here ` 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` it seems like the activity created after the fragment or similar like that. – noman404 Mar 23 '18 at 15:35
0

Go to This Link for getting information for the https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

Put different layout for rotation, screen size changes according:

res/layout/layout.xml //default layout of app

res/layout-land/layout.xml //layout when rotated.

res/layout-w600dp/layout //layout when width of screen increases the 600dp limit

Kousei
  • 1,291
  • 1
  • 9
  • 16
  • Thank you. I tried it before, that way the activity will be destroyed and then created again, but I will lose the current state.(say, A cursor, and I don't know how to save that) As a result of which, the only thing I can think of is to overrides the method. Can you think of any better idea? – Shun Vedeus Mar 23 '18 at 11:13
  • https://developer.android.com/guide/components/activities/activity-lifecycle.html look into it or try to use saveInstanceState and restoreInstance method – Kousei Mar 23 '18 at 17:20
0

Thanks all for willing to help. My Problem was solved just now, it's something about the function "setContentView()". (I didn't know where exactly,any elaborations would be appreciated.) But I did manage to make it by changing a few lines concerning that function:

        LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        view=inflater.inflate(R.layout.activity_main,null);
        viewLand=inflater.inflate(R.layout.activity_main_land,null);

and in "onConfigurationChanged()":

        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            setContentView(viewLand);
        }else{
            setContentView(view);
        }

All in all, can't use the same "R.layout.activity_main" twice in "setContentView()". I hope this will help someone who unfortunately get the same problem as mine.