1

I'm trying to build a settings activity for my Android App, it works on portrait but when I turn on landscape mode it crashes

Here's my code

<?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();
} }

and error log

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.access$900(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.access$900(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.access$900(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) 

I don't know how to solve it, why on portrait works?

4

2 回答 2

1

You do not need the following piece of code

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

since your fragment is already defined in the XML.

于 2015-09-24T19:08:57.400 回答
0

Override onCreateView, not the onCreate method in your SettingsFragment class.

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

    return fragmentView;
}
于 2015-09-24T19:03:23.983 回答