Activity 横向设置片段崩溃

Activity with Settings Fragment crash on landscape

我正在尝试为我的 Android 应用构建设置 activity,它可以在纵向模式下运行,但是当我打开横向模式时它会崩溃

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content">

<fragment     android:name="project.android.udacity.com.popularmovies.app.SettingsFragment"
    android:id="@+id/settings_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</fragment>
</LinearLayout>


public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_general);
} }

public class SettingsActivity extends AppCompatActivity {

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

    getFragmentManager().beginTransaction()
            .replace(R.id.settings_content, new SettingsFragment())
            .commit();
} }

和错误日志

09-24 20:31:12.678 8883-8883/? E/AndroidRuntime: FATAL EXCEPTION: main
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime: Process:           project.android.udacity.com.popularmovies.app, PID: 8883
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{project.android.udacity.com.popularmovies.app/project.android.udacity.com.popularmovies.app.SettingsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3930)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.access0(ActivityThread.java:151)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5299)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at project.android.udacity.com.popularmovies.app.SettingsActivity.onCreate(SettingsActivity.java:14)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5264)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3930)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.access0(ActivityThread.java:151) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5299) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Fragment project.android.udacity.com.popularmovies.app.SettingsFragment did not create a view.
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.Activity.onCreateView(Activity.java:4842)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:41)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at project.android.udacity.com.popularmovies.app.SettingsActivity.onCreate(SettingsActivity.java:14) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5264) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3930) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.access0(ActivityThread.java:151) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5299) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 
09-24 20:31:12.678 8883-8883/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

不知道怎么解决,为什么在人像作品上?

覆盖 onCreateView,而不是 SettingsFragment class 中的 onCreate 方法。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View fragmentView = inflater.inflate(R.layout.your_fragment_layout, container, true);

    return fragmentView;
}

你不需要下面这段代码

getFragmentManager().beginTransaction()
        .replace(R.id.settings_content, new SettingsFragment())
        .commit();

因为您的片段已经在 XML 中定义。