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

Dialog onClick Crash sometimes #1262

Closed
ankitagrawal2411 opened this Issue Jan 16, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@ankitagrawal2411
Contributor

ankitagrawal2411 commented Jan 16, 2017

java.lang.ArrayIndexOutOfBoundsException: length=1; index=-1
       at java.util.ArrayList.get(ArrayList.java:310)
       at com.afollestad.materialdialogs.DefaultRvAdapter$DefaultVH.onClick(DefaultRvAdapter.java:172)
       at android.view.View.performClick(View.java:5201)
       at android.view.View$PerformClick.run(View.java:21215)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5525)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

The cause:

@Override
        public void onClick(View view) {
            if (adapter.callback != null) {
                CharSequence text = null;
                if (adapter.dialog.builder.items != null && getAdapterPosition() < adapter.dialog.builder.items.size())
                    text = adapter.dialog.builder.items.get(getAdapterPosition());
                adapter.callback.onItemSelected(adapter.dialog, view, getAdapterPosition(), text, false);
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (adapter.callback != null) {
                CharSequence text = null;
                if (adapter.dialog.builder.items != null && getAdapterPosition() < adapter.dialog.builder.items.size())
                    text = adapter.dialog.builder.items.get(getAdapterPosition());
                return adapter.callback.onItemSelected(adapter.dialog, view, getAdapterPosition(), text, true);
            }
            return false;
}

In Both onClick and onLongClick method you are using getAdapterPosition() and it can return -1
sometimes as per docs .
Please add a check to getAdapterPosition()!=RecyclerView.NO_POSITION in both the methods

If you want I could make a PR

ankitagrawal2411 added a commit to ankitagrawal2411/material-dialogs that referenced this issue Jan 16, 2017

@ankitagrawal2411

This comment has been minimized.

Show comment
Hide comment
@ankitagrawal2411

ankitagrawal2411 Jan 17, 2017

Contributor

resolved via merge so closing

Contributor

ankitagrawal2411 commented Jan 17, 2017

resolved via merge so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment