其他分享
首页 > 其他分享> > android – 尝试更改设备配置时,找不到id XXX的视图

android – 尝试更改设备配置时,找不到id XXX的视图

作者:互联网

我遇到了不同布局和设备配置更改的问题.

应用程序有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) 

当我将设备方向从横向更改为纵向时,会出现此异常.因此,当第一次启动时,一切都很好,旋转设备进入横向模式也很好,但当我将其旋转回肖像时,它会因给定的异常而崩溃.

这是我的布局结构:
Layout structure

我正在调试它,发现feed活动布局正确膨胀,甚至SubNodesFragment的布局也正确膨胀,但是它崩溃了,我无法弄清楚它究竟在哪里崩溃.

解决方法:

如果SubNodesFragment上有setRetainInstance(true),则会导致此错误.这是怎么回事:

>活动开始,创建您的FeedsFragment并将其添加到feeds_fragment_container – 我们不在横向,因此没有SubNodeFragment.
>旋转屏幕(破坏活动) – 保留或重新创建FeedsFragment以及新的SubNodesFragment,因为我们现在处于横向方向.
>再次旋转屏幕(返回肖像 – 活动被破坏).您得到异常,因为在SubNodesFragment上设置了setRetainInstanceState(true).该片段将看起来被恢复到它所放入的原始视图ID(R.id.subnodes_fragment_container),它不存在;导致异常.

删除SubNodesFragment上的setRetainInstance(true)将防止发生此异常.

标签:android,android-layout,android-fragments,android-orientation
来源: https://codeday.me/bug/20190611/1219916.html