recycleView中引用v4包出错处理

前端之家收集整理的这篇文章主要介绍了recycleView中引用v4包出错处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

java.lang.NoSuchMethodError: No static method getDisplay(Landroid/view/View;)Landroid/view/Display; in class Landroid/support/v4/view/ViewCompat; or its super classes (declaration of 'android.support.v4.view.ViewCompat' appears in /data/app/com.net.search-1/base.apk:classes16.dex)
at me.support.v7.widget.RecyclerView.onAttachedToWindow(RecyclerView.java:2537)
at android.view.View.dispatchAttachedToWindow(View.java:14798)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2962)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1618)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1340)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6809)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5932)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:816)

上面错误的意思是,在公司自定义的recyclerView这个控件中,在onAttachedToWindow这个方法中,用到了v4包中的ViewComat的getDisplay,而在目前引用的v4包中没有这个方法:

recyclerView中的方法

protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mLayoutOrScrollCounter = 0;
        mIsAttached = true;
        mFirstLayoutComplete = mFirstLayoutComplete && !isLayoutRequested();
        if (mLayout != null) {
            mLayout.dispatchAttachedToWindow(this);
        }
        mPostedAnimatorRunner = false;

        if (ALLOW_THREAD_GAP_WORK) {
            // Register with gap worker
            mGapWorker = GapWorker.sGapWorker.get();
            if (mGapWorker == null) {
                mGapWorker = new GapWorker();

                // break 60 fps assumption if data from display appears valid
                // NOTE: we only do this query once,statically,because it's very expensive (> 1ms)
                Display display = ViewCompat.getDisplay(this);
                float refreshRate = 60.0f;
                if (!isInEditMode() && display != null) {
                    float displayRefreshRate = display.getRefreshRate();
                    if (displayRefreshRate >= 30.0f) {
                        refreshRate = displayRefreshRate;
                    }
                }
                mGapWorker.mFrameIntervalNs = (long) (1000000000 / refreshRate);
                GapWorker.sGapWorker.set(mGapWorker);
            }
            mGapWorker.add(this);
        }
    }

原因是,在工程的compile中引入recyclerView的时候,去掉了v4包的引用:
compile('com.me.sdk:fme-recyclerview:2.0.+') {
        exclude module: 'support-v4'
        exclude module: 'support-annotations'
        exclude module: 'me-support-v4'
    }
exclude表示,去掉这个module的依赖
所以就找不到v4包里的那个方法了.

于是我把这个注释掉:

compile('com.mei.sdk:fme-recyclerview:2.0.+') {
        /*exclude module: 'support-v4'*/
        exclude module: 'support-annotations'
        exclude module: 'me-support-v4'
    }
这个异常就消失了

另外,附上,gradle依赖的学习资料:

跟我学Gradle-5.3:依赖-管理依赖的版本(传递(transitive)\排除(exclude)\强制(force)\动态版本(+))

原文链接:https://www.f2er.com/javaschema/282710.html

猜你在找的设计模式相关文章