Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crash when data count is less than columns count in GridDividerItemDecoration #28

Open
adammo1990 opened this issue Aug 22, 2016 · 4 comments

Comments

@adammo1990
Copy link

App crash when data count is less than columns count (if I'm using GridDividerItemDecoration)
Below log:

08-22 06:45:15.364 5852-5852/pl.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.myapp, PID: 5852
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at com.dgreenhalgh.android.simpleitemdecoration.grid.GridDividerItemDecoration.drawHorizontalDividers(GridDividerItemDecoration.java:84)
at com.dgreenhalgh.android.simpleitemdecoration.grid.GridDividerItemDecoration.onDraw(GridDividerItemDecoration.java:43)
at android.support.v7.widget.RecyclerView.onDraw(RecyclerView.java:3658)
at android.view.View.draw(View.java:15231)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3593)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.h

@dgreenhalgh
Copy link

Hey @adammo1990, I'm currently working on a 2.0 version of the library and the sample that will take care of this issue. Thanks for the heads up!

@siju2014
Copy link

siju2014 commented Sep 12, 2016

This can be used as a temporary fix if needed
`

   private void drawHorizontalDividers(Canvas canvas, RecyclerView parent) {
           int childCount = parent.getChildCount();
           int rowCount = childCount / mNumColumns;
           int lastRowChildCount = childCount % mNumColumns;
           int newChildCount;
            **if (childCount < mNumColumns) {
                 newChildCount = childCount;
               } else {
                newChildCount = mNumColumns;
               }**
    for (int i = 1; i < **newChildCount**; i++) {..}`

@ysmintor
Copy link

I try to decode the math in this calculation. Finally, I found I solve the problem. Here, I add some check in drawHorizalDividers() and drawVerticalDividers().And it is better use those to methods in onDrawOver().
But there still need someone try to find why no the first column items still have some tiny left margin in my case.

You can find the gist file in :https://gist.github.com/ysmintor/b974a7e0ab155cc9d9d8818182626e05

@mobiRic
Copy link

mobiRic commented Mar 2, 2018

Apologies I have duplicated this in #33

I submitted a PR for it in #36 before reading the comment about an upcoming v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants