在旋转时没有找到片段的视图

时间:2015-12-30 07:08:17

标签: android fragment

1 / activity_main in" layout"文件夹:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_normal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.demofragment.MainActivity" >
</FrameLayout>

2 / activity_main in&#34; layout-land&#34;文件夹:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    tools:context="com.example.demofragment.MainActivity" >

    <com.example.supportlibrary.MenuLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_sliding_menu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.example.supportlibrary.MenuLayout>

</FrameLayout>

3 / FragmentOne.class:

public class FragmentOne extends Fragment implements OnClickListener{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_one_layout, container,false);
        return v;
     }
}

4 / FragmentTwo.class:

 public class FragmentTwo extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_two_layout, container,false);
        return v;
    }
}

5 / MainActivity.class:

public class MainActivity extends FragmentActivity {
    FragmentTransaction transaction;
    FragmentOne frg1;
    FragmentTwo frg2;
    FragmentManager manager; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frg1 = new FragmentOne();
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.layout_normal, frg1, "Frag_One");
        transaction.commit();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            frg1 = new FragmentOne();
            frg2 = new FragmentTwo();
            transaction.add(R.id.layout_sliding_menu, frg1, "Frag_One");
            transaction.add(R.id.layout_sliding_menu, frg2, "Frag_Two");
            transaction.commit();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            frg1 = new FragmentOne();
            transaction.add(R.id.layout_normal, frg1, "Frag_One");
            transaction.commit();
        }
    }

问题是我在旋转屏幕时遇到此异常:

12-30 06:54:40.062: E/AndroidRuntime(1641): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demofragment/com.example.demofragment.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f090000 for fragment FragmentOne{53512438 #1 id=0x7f090000 Frag_Top_tag}

请告诉我如何解决它!谢谢!

1 个答案:

答案 0 :(得分:0)

配置更改时再次调用setContentView。 由于您自己处理配置更改,因此需要再次设置内容视图,以便使用正确的布局。

相关问题