问题
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
回答1:
Move the setRequestedOrientation() after the add/clearFlags() code
Edit: as stated below, I didn't see that it's using a preferenceActivity. Just for your understanding, this is the PreferenceActivity.onCreate() which you call with super.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);
}
Why you request FEATURE_NO_TITLE if this already is requested in the super.onCreate()? Sometimes it's very helpful if you dig into the Android source code.
回答2:
In this case the super is setting the content view, and you must use requestWindowFeature before setting the content view. Thus use requestWindowFeature before calling the super.
public class LandNavSettings extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle icicle) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(icicle);
}
}
来源:https://stackoverflow.com/questions/4813831/requestwindowfeaturewindow-feature-no-title-gives-the-exception