requestWindowFeature(Window.FEATURE_NO_TITLE); 给出了例外

import android.content.pm.ActivityInfo; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.util.Log; import android.view.Window; import android.view.WindowManager; public class OptionsActivity extends PreferenceActivity { private ListPreference mListPreference; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); addPreferencesFromResource(R.xml.options); mListPreference = (ListPreference) findPreference("listpreference"); mListPreference.setPersistent(false); } } Exception Stacktrace is given below: 01-27 12:35:51.920: ERROR/AndroidRuntime(615): FATAL EXCEPTION: main 01-27 12:35:51.920: ERROR/AndroidRuntime(615): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.game/com.android.game.OptionsActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.os.Handler.dispatchMessage(Handler.java:99) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.os.Looper.loop(Looper.java:123) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at java.lang.reflect.Method.invoke(Method.java:521) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at dalvik.system.NativeStart.main(Native Method) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.Activity.requestWindowFeature(Activity.java:2719) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.game.OptionsActivity.onCreate(OptionsActivity.java:20) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-27 12:35:51.920: ERROR/AndroidRuntime(615): ... 11 more 

在add / clearFlags()代码之后移动setRequestedOrientation()

编辑:如下所述,我没有看到它使用了preferenceActivity。 只是为了您的理解,这是您使用super.onCreate()调用的PreferenceActivity.onCreate():

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(com.android.internal.R.layout.preference_list_content); mPreferenceManager = onCreatePreferenceManager(); getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); } 

如果在super.onCreate()中已经请求了FEATURE_NO_TITLE,为什么还要求它? 有时,如果您深入了解Android源代码,这将非常有用。

在这种情况下,super正在设置内容视图,您必须在设置内容视图之前使用requestWindowFeature。 因此在调用super之前使用requestWindowFeature。

 public class LandNavSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @Override protected void onCreate(Bundle icicle) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(icicle); } }