我遇到了不同布局和设备配置更改的问题。
App有3个feeds_activity布局(默认,横向和平板电脑)。
默认值:
<?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>
风景:
<?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>
片剂:
<?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>
这是一个onCreate方法的活动:
@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);
}
}
这是一个例外:
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)
当我将设备方向从横向更改为纵向时,会出现此异常。因此,当第一次启动时,一切都很好,旋转设备进入横向模式也很好,但当我将其旋转回肖像时,它会因给定的异常而崩溃。
我正在调试它,发现Feed活动布局正确膨胀,甚至SubNodesFragment
的布局也正确膨胀,但是它崩溃了,我无法确定崩溃的确切位置。
答案 0 :(得分:7)
如果setRetainInstance(true)
上有SubNodesFragment
,则会导致此错误。会发生什么:
FeedsFragment
并将其添加到feeds_fragment_container
- 我们不在风景中,因此没有SubNodeFragment
。FeedsFragment
以及新的SubNodesFragment
,因为我们现在处于横向方向。setRetainInstanceState(true)
上设置了SubNodesFragment
。该片段将看起来被恢复到它所放入的原始视图ID(R.id.subnodes_fragment_container
)中,该ID不存在;造成例外。删除setRetainInstance(true)
上的SubNodesFragment
可以防止发生此异常。
答案 1 :(得分:0)
@Alex Townsend的答案是正确的,以防您不想setRetainInstance(true)
。
如果您打算使用setRetainInstance(true)
,以便可以在UI的不同时间/状态(例如方向更改)以不同的视图(容器)显示片段实例/状态,那么我们需要一些手动操作处理片段的实例或状态。
由于片段管理器不允许2个用例,因此需要这样做:
IllegalStateException: Can't change container ID of Fragment
java.lang.IllegalArgumentException: No view found for id XXXXX for fragment YYYY
在我的情况下,我想在“纵向”模式下的tablayout(viewpager)中使用相同的片段,在横向模式下,应将其从“ tablayout”中移到另一个视图中。
修复:您需要一个事务来从片段管理器中删除片段,同时保留片段实例/状态,并创建一个新的添加/替换事务以将实例/状态设置为新(相同/不同)视图。有关代码解决方案,您可以参考答案this question。如果您正在关注this answer,请记住您拥有的片段实例是新的,因此需要在片段的onCreate中手动读取保存的状态并将UI状态重置为上一个。