gpt4 book ai didi

android - 第一次调用 getDecorView 会锁定 View 。如何知道 View 是否被锁定?

转载 作者:行者123 更新时间:2023-11-29 20:22:54 24 4
gpt4 key购买 nike

这是 Window.getDecorView() 的文档 http://developer.android.com/reference/android/view/Window.html#getDecorView()

据此,当第一次调用 API 时,各种窗口特性处于锁定模式。这很好。但是有没有办法检查窗口中的当前 View 是否处于这种锁定状态?我正在尝试调用这样的方法:

private void replaceView() {
Window window = getActivity().getWindow();
WindowManager wm = getActivity().getWindowManager();
wm.removeViewImmediate(window.getDecorView());
wm.addView(window.getDecorView(), window.getAttributes());
}

现在在某些用例中(比如方向改变),当第一次调用上述方法时,我会崩溃。

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{eebbc2d V.ED.... R.....ID 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
at com.airwatch.inbox2015.ui.email.MessageComposeFragment.replaceView(MessageComposeFragment.java:619)

是否可以通过某种方式知道我是第一次调用 getDecorView(),因此 removeViewImmediate() API 可能无法工作,从而避免调用该 API?

感谢任何帮助。

最佳答案

使用 peekDecorView 发现锁定状态。它将“检索当前的装饰 View ,但前提是它已经被创建”。例如,在您的 Activity 中:

View decorView = getWindow().peekDecorView();
if( decorView == null )
{
// Not created yet, maybe because setContentView was not called.
// Calling getDecorView at this point forces creation, but also
// "locks in various window characteristics", maybe prematurely.
// You probably want to avoid that.
}
else
{
// Window characteristics are locked in, and decorView is safely
// ready to use.
}

使用 peekDecorView 避免了 getDecorView 的副作用。如果此方法不能解决您的崩溃问题,那么希望它能让您更接近解决方案。

关于android - 第一次调用 getDecorView 会锁定 View 。如何知道 View 是否被锁定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com