Android:新片段在方向更改时崩溃

Android: new Fragment crashes on Orientation Change

当屏幕方向改变时,我的 Fragment 目前遇到问题。 我已经尝试过 android:configChanges="orientation" 之类的方法,但没有用。 所以问题是:当应用程序在 FragmentContainer 中加载新的 Fragment 时,它会崩溃并显示以下日志:

>java.lang.RuntimeException: Unable to start activity ComponentInfo{foo.bar/foo.bar.MainActivity}: android.view.InflateException: Binary XML file line #13: 
Binary XML file line #30: Error inflating class fragment  
Caused by: android.view.InflateException: Binary XML file line #13: Binary XML file line #30: Error inflating class fragment   
    Caused by: android.view.InflateException: Binary XML file line #30: 
    Error inflating class fragment   
    Caused by: java.lang.IllegalStateException: Fragment foo.bar.BlankFrag did not create a view.

错误的来源是 setContentView(R.layout.activity_main) 方法。

相关的 activity_main.xml 摘录:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/adView">
   <fragment
       android:id="@+id/FragFaecher"
       class="foo.bar.BlankFrag"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </fragment>
</FrameLayout>

BlankFrag.class:

public class BlankFrag extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

    return rootView;

    }
}

fragment_blank.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/ScimaBackground">


</LinearLayout>

相关摘录自MainActivity.class

@Override
    protected void onCreate (Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);

        if(savedInstanceState != null){
            Intent intent = new Intent(getApplicationContext(), SplashScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }


        boolean firstRun = getSharedPreferences("preferences", MODE_PRIVATE).getBoolean("firstrun", true);
        if(firstRun){
            getSharedPreferences("preferences", MODE_PRIVATE).edit().putBoolean("firstrun", false).commit();
            new dbconnect(this).updateDB();
        }


        setContentView(R.layout.activity_main);

Fragment fach = new FaecherFrag(); //new Fragment that gets loaded

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        ft.replace(R.id.FragFaecher, fach).commit();

如果您想动态加载片段,您使用的 <fragment> 标签有误

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

    ...

    Fragment fach = new FaecherFrag(); //new Fragment that gets loaded
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    // This should be the ID of a FrameLayout
    ft.replace(R.id.FragFaecher, fach).commit();

所以,像这样改变你的XML

<FrameLayout
    android:id="@+id/FragFaecher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/adView">
</FrameLayout>