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
Asked
Active
Viewed 4.4k times
7
2 Answers
12
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.

Oliver
- 1,269
- 13
- 21
-
3Oh yes, I just saw you extend PreferenceActivity. Hard to read the unformatted text. You can not do that with extending the PreferenceActivity. As soon as you call super.onCreate(), the ViewGroup will be set up and so, you are not allowed to change the Window's parameters. You COULD try to call super.onCreate() at a later state (after the window flag settings), but I doubt you can access the Window's member at that state of creation. – Oliver Jan 27 '11 at 08:10
-
I edited my answer so you can see what happens on super.onCreate() – Oliver Jan 27 '11 at 08:18
9
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);
}
}

Christopher Grabowski
- 131
- 1
- 2