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

前端之家收集整理的这篇文章主要介绍了android – requestWindowFeature(Window.FEATURE_NO_TITLE);给出了例外前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. import android.content.pm.ActivityInfo;
  2. import android.os.Bundle;
  3. import android.preference.ListPreference;
  4. import android.preference.Preference;
  5. import android.preference.PreferenceActivity;
  6. import android.util.Log;
  7. import android.view.Window;
  8. import android.view.WindowManager;
  9.  
  10. public class OptionsActivity extends PreferenceActivity {
  11. private ListPreference mListPreference;
  12.  
  13. @Override
  14. protected void onCreate (Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16.  
  17. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  18. requestWindowFeature(Window.FEATURE_NO_TITLE);
  19. getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  20. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  21.  
  22. addPreferencesFromResource(R.xml.options);
  23. mListPreference = (ListPreference) findPreference("listpreference");
  24. mListPreference.setPersistent(false);
  25.  
  26. }
  27. }
  28.  
  29. Exception Stacktrace is given below:
  30. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): FATAL EXCEPTION: main
  31. 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
  32. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  33. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  34. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  35. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  36. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.os.Handler.dispatchMessage(Handler.java:99)
  37. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.os.Looper.loop(Looper.java:123)
  38. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.main(ActivityThread.java:4627)
  39. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at java.lang.reflect.Method.invokeNative(Native Method)
  40. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at java.lang.reflect.Method.invoke(Method.java:521)
  41. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  42. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  43. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at dalvik.system.NativeStart.main(Native Method)
  44. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
  45. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172)
  46. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.Activity.requestWindowFeature(Activity.java:2719)
  47. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at com.android.game.OptionsActivity.onCreate(OptionsActivity.java:20)
  48. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  49. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  50. 01-27 12:35:51.920: ERROR/AndroidRuntime(615): ... 11 more

解决方法

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

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

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4.  
  5. requestWindowFeature(Window.FEATURE_NO_TITLE);
  6.  
  7. setContentView(com.android.internal.R.layout.preference_list_content);
  8.  
  9. mPreferenceManager = onCreatePreferenceManager();
  10. getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
  11. }

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

猜你在找的Android相关文章