java.lang.NoClassDefFoundError的:无法初始化类android.view.GestureDetector初始化、NoClassDefFoundError、lang、java

2023-09-06 10:27:49 作者:守侯-LOVE

我想初始化一个水平滚动视图类时收到此错误:

I am getting this error when trying to initialize a horizontal scroll view class:

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);
    }

错误被追溯到这一行:

the error is being traced back to this line:

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

mGesture是一家民营GestureDetector:

mGesture is a private GestureDetector:

private GestureDetector mGesture;

推荐答案

您不作为尖端说需要它时,在编辑模式下,无论如何,这样做的:

You don't need it when in edit mode anyway, so do as the tip says:

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是在code空使用前。

And then check for mGesture and mCcroller being null in the code before using.