位置: 编程技术 - 正文

关于使用RequestWindowFeature为啥一定要在setContentView之前调用

编辑:rootadmin

推荐整理分享关于使用RequestWindowFeature为啥一定要在setContentView之前调用,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

网上关于RequestWindowFeature()的用法有很多,却难找一篇解释清楚的文章供大家了解,下面这是我遇到这个问题并且得出的一点结论供大家参考。

Activity的RequestWindowFeature()实际上走的是PhoneWIndow的requestFeature(),在PhoneWIndow的requestFeature()中有个前提条件,成员属性mContentParent不能为非null,这是构建窗体的view,即在为窗体设置属性时,还不能构建窗体。如下:

@Override public boolean requestFeature(int featureId) { if (mContentParent != null) { throw new AndroidRuntimeException("requestFeature() must be called before adding content"); }

.....

return super.requestFeature(featureId); }

但是在activity中使用setContentView()时,实际上走的是phonewindow的setContentView,根据代码我们看到

关于使用RequestWindowFeature为啥一定要在setContentView之前调用

@Override

public void setContentView(View view, ViewGroup.LayoutParams params) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mContentParent.addView(view, params); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); }

}

它会首先判断mContentParent 是否为null,如果为null,进入installDecor();

private void installDecor() {

......

if (mContentParent == null) { mContentParent = generateLayout(mDecor); mTitleView = (TextView)findViewById(com.android.internal.R.id.title); if (mTitleView != null) { if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) { View titleContainer = findViewById(com.android.internal.R.id.title_container); if (titleContainer != null) { titleContainer.setVisibility(View.GONE); } else { mTitleView.setVisibility(View.GONE); } if (mContentParent instanceof FrameLayout) { ((FrameLayout)mContentParent).setForeground(null); } } else { mTitleView.setText(mTitle); } }

.....

}

这是installDecor方法的部分代码,从中我们可以看到,它会对mContentParent 进行初始化,从而赋予相应的&#;,这就是RequestWindowFeature为啥一定要在setContentView之前调用就会抛此类异常

Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content- ::.: E/AndroidRuntime(): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:)- ::.: E/AndroidRuntime(): at android.app.Activity.requestWindowFeature(Activity.java:)- ::.: E/AndroidRuntime(): at com.solar.BaseActivity.onCreate(BaseActivity.java:)- ::.: E/AndroidRuntime(): at com.solar.SetupInfoActivity.onCreate(SetupInfoActivity.java:)- ::.: E/AndroidRuntime(): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:)- ::.: E/AndroidRuntime(): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:)d

Android 性能测试实践(二) 实时监控工具 转载地址:

Android开发之时间刻度盘 一、最近的一个项目中有遇到时间刻度盘的需求,在网上没找到合适的,于是自己就花点时间实现了,现在分享出来,效果如下图:在介绍如何实现之

Android面试题 1.Android序列化,其中的差异。Android自定义对象可序列化有两个选择一个是Serializable和Parcelable;差异:1、在使用内存的时候,Parcelable比Serializable性能高

本文链接地址:https://www.jiuchutong.com/biancheng/382298.html 转载请保留说明!

上一篇:自定义ListView使用Volley获取数据(android自定义listview)

下一篇:Android 性能测试实践(二) 实时监控工具(安卓app性能测试)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络