在recyclerview上设置onScrollChangeListener时的java.lang.NoClassDefFoundError

时间:2016-04-28 14:34:41

标签: android android-recyclerview

我试图让这个onScrollListener适用于23以下的api版本,当我尝试这样做时,我得到了我在标题中提到的错误:

我在这里收到错误:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        list.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                    //If scrolled at last then
                if (isLastItemDisplaying(recyclerView)) {
                    // Calling the method get data again
                    getData();
                }
            }
        });
    } else {
        // here i'm getting an error
        list.setOnScrollChangeListener(new RecyclerView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    // If scrolled at last then
                if (isLastItemDisplaying(list)) {
                    // Calling the method get data again
                    getData();
                }
            }
        });
    }

我启用了multidex支持并编译'com.android.support:multidex:1.0.1',但我仍然收到错误消息。我也在类中初始化MultiDex,扩展了应用程序,如下所示:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

编辑:

                                                                                   Process: com.dusandimitrijevic.pdssolution, PID: 13882
                                                                               java.lang.NoClassDefFoundError: com.dusandimitrijevic.pdssolution.fragments.JokesFragment$1                                                                                 at com.dusandimitrijevic.pdssolution.fragments.JokesFragment.onCreateView(JokesFragment.java:99)
                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
                                                                                   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
                                                                                   at android.view.View.measure(View.java:18596)
                                                                                   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
                                                                                   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:590)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                                a

2 个答案:

答案 0 :(得分:1)

在API级别23中添加了View.OnScrollChangeListener。 解决方法是使用

mRecycleView.getViewTreeObserver().addOnScrollChangedListener(listenerInstance);

如果您尝试获取X,Y值,上述回调侦听器将没有多大帮助。

答案 1 :(得分:0)

RecyclerViewcom.android.support:recyclerview-v7库的一部分。你是否添加了依赖项?

compile 'com.android.support:recyclerview-v7:21.0.+'