2019-01-29-VIew绘制异常

调试自定义View显示的时候遇到如下问题:

01-29 18:43:49.517 2985-2985/com.horizion.ai.launcher E/FloatWindowUtils: showTTSWindow: 
    java.lang.IllegalStateException: View android.support.constraint.ConstraintLayout{c571b16 V.E...... ......ID 0,0-1920,143} has already been added to the window manager.
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:313)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at com.horizon.xos.customsdk.view.FloatWindowUtils$7.run(FloatWindowUtils.java:202)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:792)

主要原因是View在加载的时候,其父控件已经被销毁了,导致绘制或者销毁失败,父控件可能是Activity或者View。主要规避方式,在绘制时判断父控件是否为null在确定是否绘制或者销毁。

if(view.getView().getParent() != null) {
    windowManager.addView(view.getView(), view.getLayoutParams());
 }

你可能感兴趣的:(2019-01-29-VIew绘制异常)