1

尝试初始化水平滚动视图类时出现此错误:

java.lang.NoClassDefFoundError: Could not initialize class android.view.GestureDetector

private synchronized void initView() {
        mLeftViewIndex = -1;
        mRightViewIndex = 0;
        mDisplayOffset = 0;
        mCurrentX = 0;
        mNextX = 0;
        mMaxX = Integer.MAX_VALUE;
        mScroller = new Scroller(getContext());
        mGesture = new GestureDetector(getContext(), mOnGesture);
    }

错误被追溯到这一行:

mGesture = new GestureDetector(getContext(), mOnGesture);

mGesture 是一个私有的 GestureDetector:

private GestureDetector mGesture;

我的用户界面

4

1 回答 1

1

无论如何,在编辑模式下你都不需要它,所以按照提示说:

private synchronized void initView() {
    mLeftViewIndex = -1;
    mRightViewIndex = 0;
    mDisplayOffset = 0;
    mCurrentX = 0;
    mNextX = 0;
    mMaxX = Integer.MAX_VALUE;
    if (!this.isInEditMode()) {
        mScroller = new Scroller(getContext());
        mGesture = new GestureDetector(getContext(), mOnGesture);
    }
}

然后在使用前检查代码中的 mGesture 和 mCcroller 是否为空。

于 2013-05-31T12:00:08.593 回答