Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No view found for id XXX when trying to change device configuration

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.

like image 533
Olexii Muraviov Avatar asked Mar 12 '23 13:03

Olexii Muraviov


1 Answers

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.

like image 193
Alex Townsend Avatar answered Apr 09 '23 13:04

Alex Townsend