马会东的博客

马会东的博客

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用SafeViewFlipper避免ViewFlipper交替时Crash

ViewFilpper 是Android官方提供的一个View容器类,继承于ViewAnimator类,用于实现页面切换。当我们界面重叠较多的时候,ViewFilpper 容易崩溃,直接导致程序Crash。

我们可以使用自定义的SafeViewFlipper来避免这一问题。

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ViewFlipper;

public class SafeViewFlipper extends ViewFlipper {

    public SafeViewFlipper(Context context) {
        super(context);
    }

    public SafeViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onDetachedFromWindow() {
        try {
            super.onDetachedFromWindow();
        }
        catch (IllegalArgumentException e) {
            // This happens when you're rotating and opening the keyboard that the same time
            // Possibly other rotation related scenarios as well
            stopFlipping();
        }
    }
}

 

posted on 2015-05-20 22:21  马会东  阅读(330)  评论(0编辑  收藏  举报