我在尝试初始化水平 ScrollView 类时遇到此错误:
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 是私有(private) GestureDetector:
private GestureDetector mGesture;
最佳答案
无论如何在编辑模式下你都不需要它,所以按照提示去做:
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是否为null。
关于android - java.lang.NoClassDefFoundError : Could not initialize class android. View .GestureDetector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856497/