簡體   English   中英

找不到旋轉片段的視圖

[英]No view found for fragment on rotation

1 / activity_main位於“布局”文件夾中:

<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位於“ layout-land”文件夾中:

<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}

請告訴我如何解決它!謝謝!

配置更改時,再次調用setContentView。 由於您自己處理配置更改,因此需要再次設置內容視圖,以便使用正確的布局。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM