1

我想要纵向和横向模式的不同布局。此外,我希望我的片段在方向发生变化时保存它们的状态。这里是layout\activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

它是layout-land\activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentorientationchangetest.Fragment1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentorientationchangetest.Fragment2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

这里是MainActivity

public class MainActivity extends Activity {

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

        if (findViewById(R.id.container) != null) {

            if (savedInstanceState != null) {
                return;
            }
            Fragment1 fragment1 = new Fragment1();
            getFragmentManager().beginTransaction()
                    .add(R.id.container, fragment1, "tag1").commit();
        }

    }

它是Fragment1:

public class Fragment1 extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View  view = inflater.inflate(R.layout.fragment1, container, false);

        return view;
    }

}

Fragment2 与 Fragment1 相同。

但是,当我以纵向模式开始并旋转到横向模式时,应用程序崩溃了。问题是因为使用setRetainInstance(true),它保存了对 的引用R.id.container,但横向布局中不存在此容器,这是输出:

10-02 15:52:50.679: E/AndroidRuntime(2531): FATAL EXCEPTION: main
10-02 15:52:50.679: E/AndroidRuntime(2531): Process: com.example.fragmentorientationchangetest, PID: 2531
10-02 15:52:50.679: E/AndroidRuntime(2531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentorientationchangetest/com.example.fragmentorientationchangetest.MainActivity}: 
    java.lang.IllegalArgumentException: No view found for id 0x7f090000 (com.example.fragmentorientationchangetest:id/container) for fragment Fragment1{52874ecc #0 id=0x7f090000 tag1}

但据此,必须onRetainInstance在片段中使用来处理方向变化。我现在该怎么办?

提前致谢!

4

1 回答 1

0

在这条线上:

getFragmentManager().beginTransaction()
        .add(R.id.container, fragment1, "tag1").commit();

您正在使用 ID 将 Fragment 添加到 ViewGroup 中container。当 Activity 被销毁并重新创建时,它会尝试将该 Fragment 重新附加到具有 ID 的 ViewGroup container。但是,在您的横向布局中,您没有containerViewGroup —— 您有一个在 XML 中定义的 Fragment。代替:

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.fragmentorientationchangetest.Fragment1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

和:

<FrameLayout
    android:id="@+id/container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

以便 Activity 能够重新附加 Fragment。

于 2014-10-02T17:46:03.423 回答