2015-10-02 29 views
1

下面是Window.getDecorView()的文档 http://developer.android.com/reference/android/view/Window.html#getDecorView()调用getDecorView以查看视图中的第一次锁定。如何知道视图是否被锁定?

按照这一点,当API调用用于第一次,各种窗口characterstics处于锁定模式。这很好。但有没有办法来检查窗口中的当前视图是否处于这种锁定状态? 我试图调用这样的方法:

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?

任何帮助表示赞赏。

回答

2

使用peekDecorView来发现锁定状态。它将“检索当前的装饰视图,但只有当它已经被创建”。例如,在你的活动:

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副作用。如果这种方法不能解决您的崩溃问题,那么希望它能让您更接近解决方案。