laenger / ViewPagerBottomSheet

Use ViewPagers in Bottom Sheets!
Apache License 2.0
465 stars 86 forks source link

NullPointerException #23

Open ahmed3elshaer opened 6 years ago

ahmed3elshaer commented 6 years ago

i just integrated the library and i got this Error on launching the app

FATAL EXCEPTION: main Process: com.wadeeny.ondemand, PID: 20144 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.View.isNestedScrollingEnabled()' on a null object reference at android.support.v4.view.ViewCompat$ViewCompatApi21Impl.isNestedScrollingEnabled(ViewCompat.java:1320) at android.support.v4.view.ViewCompat.isNestedScrollingEnabled(ViewCompat.java:3308) at biz.laenger.android.vpbs.ViewPagerBottomSheetBehavior.findScrollingChild(ViewPagerBottomSheetBehavior.java:609) at biz.laenger.android.vpbs.ViewPagerBottomSheetBehavior.findScrollingChild(ViewPagerBottomSheetBehavior.java:615) at biz.laenger.android.vpbs.ViewPagerBottomSheetBehavior.findScrollingChild(ViewPagerBottomSheetBehavior.java:622) at biz.laenger.android.vpbs.ViewPagerBottomSheetBehavior.onLayoutChild(ViewPagerBottomSheetBehavior.java:252) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:758) at android.view.View.layout(View.java:19590) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2488) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2204) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1390) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6754) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966) at android.view.Choreographer.doCallbacks(Choreographer.java:778) at android.view.Choreographer.doFrame(Choreographer.java:713) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:172) at android.app.ActivityThread.main(ActivityThread.java:6637) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

hynra commented 6 years ago

Wrap your ViewPager with NestedScrollView:

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v4.view.ViewPager
                android:id="@+id/bottom_sheet_viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.v4.widget.NestedScrollView>
gcobc12632 commented 6 years ago

@hynra this can avoid the exception. but my listview in ViewPagerBottomSheet can't scroll anymore.

laenger commented 6 years ago

Is this reproducible with (corresponding adjustments inside) the example app from this repository?

huzaifaiftikhar commented 6 years ago

@laenger I'm also getting the same issue. If I use the same layout in the example app it works. However in my app it crashes with this error. If I remove viewpager from my layout the app doesn't crash. However on adding the viewpager it crashes! I'm trying to integrate your library in Open Food Facts android app.

laenger commented 6 years ago

Looking at this again, it seems that #24 fixed this.

huzaifaiftikhar commented 6 years ago

I tried this fix already before it was merged. It seems to fix the error since we do a null check however the viewpager doesn't scroll anymore. If I try to scroll down the bottom sheet collapses. Maybe I am doing something wrong. Please help!

laenger commented 6 years ago

Again, without a minimal reproducible example, it is very difficult to dig deeper into this. It would be great if you could modify the example app such that it runs into the described issue.

danponce commented 6 years ago

I was having the same crash and it was because I wasn't adding any fragment to the view pager. Be sure to check that you're adding some fragment to the view pager so it can initiate right

jobburg commented 6 years ago

I have the same problem and a reprodcution path. You need to use a PagerAdapter instead of a FragmentPagerAdapter to reproduce it.

daniellAlgar commented 6 years ago

I was also having this problem. I solved it by adding android:nestedScrollingEnabled="true" to my ViewPager.

beelancrp commented 6 years ago

@daniellAlgar This solution is not correct, because of incorrect work of behavior. I found a better solution for this bug. Try to use ViewPager.post { BottomSheetUtils.setupViewPager(viewPager) }.

Think it would be helpful. But need to understand how to fix it in lib.

beelancrp commented 6 years ago

For a better understanding, I show you how I use it. ` override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)

    pagerAdapter = StoryBoardStatsFragmentPagerAdapter(activity!!.supportFragmentManager)

    if (storyResponse.likedusers.isNotEmpty()) {
        pagerAdapter.addFragment(StoryDetailsTabFragment.newInstance(
                StoryDetailsTabFragment.TabType.LIKES, storyResponse, this),
                storyResponse.likeCount.toString().plus(" likes"))
    }

    if (storyResponse.viewedusers.isNotEmpty()) {
        pagerAdapter.addFragment(StoryDetailsTabFragment.newInstance(
                StoryDetailsTabFragment.TabType.VIEWS, storyResponse, this),
                storyResponse.viewCount.toString().plus(" views"))
    }

    viewPager.offscreenPageLimit = 1
    viewPager.adapter = pagerAdapter

    tabs.setupWithViewPager(viewPager)
}

override fun onDataLoaded() {
    viewPager.post {
        behavior = ViewPagerBottomSheetBehavior.from(container)

        behavior.isHideable = true
        behavior.setBottomSheetCallback(object : ViewPagerBottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                val percent = slideOffset * 100
                if (percent <= 45 && wasExpanded)
                    behavior.state = ViewPagerBottomSheetBehavior.STATE_HIDDEN
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED)
                    wasExpanded = true

                if (newState == BottomSheetBehavior.STATE_HIDDEN)
                    router.clearStackFragment()
            }
        })

        BottomSheetUtils.setupViewPager(viewPager) {
            router.clearStackFragment()
        }
    }
}`