我正在尝试为我的Android应用程序构建设置活动,它适用于肖像,但是当我打开横向模式时它会崩溃
这是我的代码
<?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.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)
我不知道如何解决它,为什么肖像作品?
答案 0 :(得分:1)
您不需要以下代码
getFragmentManager().beginTransaction()
.replace(R.id.settings_content, new SettingsFragment())
.commit();
因为您的片段已在XML中定义。
答案 1 :(得分:0)
覆盖onCreateView
类,而不是onCreate
类中的SettingsFragment
方法。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.your_fragment_layout, container, true);
return fragmentView;
}