android - requestWindowFeature(Window.FEATURE_NO_TITLE);给出异常(exception)

标签 android android-preferences

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

最佳答案

将 setRequestedOrientation() 移到 add/clearFlags() 代码之后

编辑:如下所述,我没有看到它使用了 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 源代码,有时会非常有帮助。

关于android - requestWindowFeature(Window.FEATURE_NO_TITLE);给出异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4813831/

相关文章:

android - 使用 Kotlin 将运行时边距设置为任何 View

java - 在 TextView 中显示评论/在编辑 View 中接受评论以在测验应用程序中使用

java - 在异步调用中更改变量

android - Flutter 中使用 dart 的视频编辑器

android - PreferenceScreen 中的屏幕停电

android - PreferenceCategory 标签的摘要字段

android - 在 Android Studio 中使用 ListPreference

android - Admob 横幅窃取焦点

java - 为什么 PreferenceCategory 的标题和复选框颜色这么浅?我该如何更改它?

安卓小工具 : Show configuration activity before widget is added to the screen