本文介绍了requestWindowFeature(Window.FEATURE_NO_TITLE);给人的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进口android.content.pm.ActivityInfo;
进口android.os.Bundle;
进口的Andr​​oid preference.List preference。
。进口的Andr​​oid preference preference;
。进口的Andr​​oid preference preferenceActivity;
进口android.util.Log;
进口android.view.Window;
进口android.view.WindowManager;

公共类OptionsActivity扩展preferenceActivity {
 私人列表$​​ P $ pference mList preference;

 @覆盖
 保护无效的onCreate(包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);

  加preferencesFromResource(R.xml.options);
  mList preference =(名单preference)找到preference(名单preference);
  mList preference.setPersistent(假);

 }
}

异常堆栈跟踪下面给出:
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):致命异常:主要
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.android.game / com.android.game.OptionsActivity}:android.util.AndroidRuntimeException:requestFeature ()添加内容之前,必须先调用
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.os.Handler.dispatchMessage(Handler.java:99)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.os.Looper.loop(Looper.java:123)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.ActivityThread.main(ActivityThread.java:4627)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在java.lang.reflect.Method.invokeNative(本机方法)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在java.lang.reflect.Method.invoke(Method.java:521)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在dalvik.system.NativeStart.main(本机方法)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):android.util.AndroidRuntimeException:产生的原因requestFeature()必须添加内容之前调用
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.Activity.requestWindowFeature(Activity.java:2719)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在com.android.game.OptionsActivity.onCreate(OptionsActivity.java:20)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12月1日至27号:35:51.920:ERROR / AndroidRuntime(615):11 ...更多
 

解决方案

添加/ clearFlags后,将setRequestedOrientation()()code

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

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    的setContentView(com.android.internal.R.layout preference_list_content。);

    米preferenceManager =的onCreate preferenceManager();
    。getListView()setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
}
 

为什么你要求FEATURE_NO_TITLE如果这已经要求在super.onCreate()?有时候,这是非常有益的,如果你深入到Android源$ C ​​$ C。

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
解决方案

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.

这篇关于requestWindowFeature(Window.FEATURE_NO_TITLE);给人的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:23