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:
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.
If you have setRetainInstance(true)
on your SubNodesFragment
it will cause this error. What happens is this:
FeedsFragment
and adds it to the feeds_fragment_container
-- we are not in landscape so there is no SubNodeFragment
.FeedsFragment
is retained or recreated, along with the new SubNodesFragment
since we are now in Landscape orientation.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With