4

I have an issue with different layouts and device configuration changes.

App has 3 feeds_activity layouts (default, landscape and for tablets).

Default:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/feeds_fragment_container">

</FrameLayout>

Landscape:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:dividerHorizontal"
    android:baselineAligned="false">

    <FrameLayout
        android:id="@+id/feeds_fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/subnodes_fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

</LinearLayout>

Tablets:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:dividerHorizontal"
    android:baselineAligned="false">

    <FrameLayout
        android:id="@+id/feeds_fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/subnodes_fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

</LinearLayout>

Here is an activity onCreate method:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feeds_activity);



        if (findViewById(R.id.subnodes_fragment_container) != null) {

            SubNodesFragment subNodesFragment = (SubNodesFragment) getSupportFragmentManager()
                    .findFragmentByTag(getString(R.string.subnodes_fragment_tag));

            if (subNodesFragment == null) {
                subNodesFragment = new SubNodesFragment();
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.subnodes_fragment_container, subNodesFragment, getString(R.string.subnodes_fragment_tag))
                        .commit();
            }
            mTabletMode = true;
        }


        FeedsFragment fragment = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.feeds_fragment_tag));


        if (fragment == null) {

            fragment = new FeedsFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.feeds_fragment_container, fragment, getString(R.string.feeds_fragment_tag))
                    .commit();
            setTabletMode(fragment);
        }
    }

    private void setTabletMode(FeedsFragment fragment) {
        if(mTabletMode && fragment != null) {
            Bundle bundle = new Bundle();
            bundle.putBoolean(getString(R.string.tablet_mode_tag), true);
            fragment.setArguments(bundle);
        }
    }

Here is an exception:

No view found for id 0x7f0d0051 (olexiimuraviov.ua.simplerssreader:id/subnodes_fragment_container) for fragment SubNodesFragment{4c8915a #1 id=0x7f0d0051 subnodes fragment}
FATAL EXCEPTION: main
 Process: olexiimuraviov.ua.simplerssreader, PID: 5277
 java.lang.RuntimeException: Unable to start activity ComponentInfo{olexiimuraviov.ua.simplerssreader/olexiimuraviov.ua.simplerssreader.ui.FeedsActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0d0051 (olexiimuraviov.ua.simplerssreader:id/subnodes_fragment_container) for fragment SubNodesFragment{4c8915a #1 id=0x7f0d0051 subnodes fragment}
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
     at android.app.ActivityThread.-wrap15(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
  Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0d0051 (olexiimuraviov.ua.simplerssreader:id/subnodes_fragment_container) for fragment SubNodesFragment{4c8915a #1 id=0x7f0d0051 subnodes fragment}
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2046)
     at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:174)
     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:598)
     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
     at android.app.Activity.performStart(Activity.java:6253)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077) 
     at android.app.ActivityThread.-wrap15(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117) 

This exception appears when I change device orientation from landscape to portrait. So when first time launch it everything is fine, rotating device into landscape mode is fine too, but when I rotate it back to portrait it crash with given exception.

Here is my layout structure: Layout structure

I was debugging it and found out that feeds activity layout inflates correctly and even layout for SubNodesFragment inflates correctly, but than it's crash and I can't figure out where exactly it crashes.

5
  • have you placed the layouts in proper folders like layout-sw600dp for 6 inch tablets and so on.. Apr 21, 2016 at 16:43
  • What resource directories are your Landscape/Tablet layouts in? Apr 21, 2016 at 17:00
  • layout-sw600dp for tablet, layout-land for landscape and just layout for default @Raghunandan Apr 21, 2016 at 17:55
  • layout-sw600dp for tablet, layout-land for landscape and just layout for default @AlexTownsend Apr 21, 2016 at 19:12
  • And what device is the error occurring on? Apr 21, 2016 at 19:25

2 Answers 2

7

If you have setRetainInstance(true) on your SubNodesFragment it will cause this error. What happens is this:

  1. Activity starts, creates your FeedsFragment and adds it to the feeds_fragment_container -- we are not in landscape so there is no SubNodeFragment.
  2. Rotate the screen (destroying the activity) -- FeedsFragment is retained or recreated, along with the new SubNodesFragment since we are now in Landscape orientation.
  3. Rotate the screen again (back to portrait -- activity is destroyed). You get the exception because setRetainInstanceState(true) is set on your SubNodesFragment. The fragment will look to be restored into the original view ID that it was put into (R.id.subnodes_fragment_container), which does not exist; causing the exception.

Removing setRetainInstance(true) on your SubNodesFragment will prevent this exception from happening.

2
  • I was looking for this for so many hours. It saved my day..Thank you!!
    – Ani
    Jun 11, 2017 at 2:18
  • @Alex Townsend while your answer is correct, what in case i intend to setRetainInstance(true)?
    – Nabster
    Nov 8, 2018 at 10:29
0

@Alex Townsend's answer is correct in case you don't intend to setRetainInstance(true).

In cases you intend to use setRetainInstance(true) so that the fragment instance/state can be shown in different views (containers) at different times/states of the UI like on orientation change then we need a little bit of manual handling of the fragment's instance or state. This needs to be done as the fragment manager does not allow 2 use cases:

  1. A Fragment which is already attached to a view A cannot be directly attached to a different view/container. In this case the following exception is thrown: IllegalStateException: Can't change container ID of Fragment
  2. A Fragment whose view/container is destroyed(let's say be config change) cannot be managed by the "FragmentManager" and throws the java.lang.IllegalArgumentException: No view found for id XXXXX for fragment YYYY on Activity's onStart

In my case i wanted to use the same fragment in a tablayout(viewpager) in Portrait mode and upon landscape mode this should be moved out of the tablayout into a different view.

The Fix: You need a transaction to remove the fragment from FragmentManager while holding the fragment instance/state and create a new add/replace transaction to set the instance/state to the new(same/different) view. For code solution you can refer the answers to this question. In case you are following this answer then please remember that the fragment instance you have is new so the saved state needs to be manually read in the onCreate of the fragment and reset the UI state to previous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.