Tablet Screen Orientation Change - No View found for id for Fragment

风格不统一 提交于 2019-11-30 19:35:49

The only way I can think of you getting that error is that your old layout is used. That could be because super.onCreate(savedInstanceState);

Instead try ignoring the saved state:

super.onCreate(null);

Edit:

Response to @matiash comment:

Since OP didn't provide code to re-create the problem, it's hard to test other (if any) solutions.

However I do agree that resetting the savedInstanceState is kind of an overkill. Therefore I think OP should try it himself and see to it that he saves as many views as possible.

The first thing that comes to mind is preventing the problematic view from being saved:

<FrameLayout
    android:id="@+id/detail_fragment_container"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="0.60"
    android:saveEnabled="false"/>
Neoh

The FragmentManager will try to recreate all the fragments when rotated, but in portrait mode you don't have the DetailFragment layout anymore, so you should remove the fragment to prevent it from being attached.

EDIT: Perhaps the cleanest way to detect your orientation change is to use the OrientationEventListener. You can refer to this post and this for some example. You should remove the fragment inside the method onOrientationChanged (int orientation) which you should override.

Change the code,

if ((findViewById(R.id.fragment_container) != null)
            && (findViewById(R.id.detail_fragment_container) != null)) {
        mTwoPane = true;
    } else {
        mTwoPane = false;
    }

by

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    //Do some stuff
mTwoPane = true;
} else{
 mTwoPane = false;
}

hope it will help you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!