From d98c02d0adc026dfca3bcc9a294ee3e9fddb80f3 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 11 Jan 2024 22:31:34 -0500 Subject: Fix NullPointerException when invoking onClick. --- .../markdown/CustomMarkwonAdapter.java | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java index 1f2ce94c..e83d5597 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/CustomMarkwonAdapter.java @@ -12,6 +12,7 @@ import android.widget.TextView; import androidx.annotation.IdRes; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.commonmark.node.Node; @@ -35,7 +36,9 @@ public class CustomMarkwonAdapter extends MarkwonAdapter { private Markwon markwon; private List nodes; + @Nullable private View.OnClickListener onClickListener; + @Nullable private View.OnLongClickListener onLongClickListener; @SuppressWarnings("WeakerAccess") @@ -50,11 +53,11 @@ public class CustomMarkwonAdapter extends MarkwonAdapter { setHasStableIds(true); } - public void setOnClickListener(View.OnClickListener onClickListener) { + public void setOnClickListener(@Nullable View.OnClickListener onClickListener) { this.onClickListener = onClickListener; } - public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) { + public void setOnLongClickListener(@Nullable View.OnLongClickListener onLongClickListener) { this.onLongClickListener = onLongClickListener; } @@ -161,25 +164,29 @@ public class CustomMarkwonAdapter extends MarkwonAdapter { } if (node instanceof ImageAndGifBlock) { - if (!holder.itemView.hasOnClickListeners()) { + if (onClickListener != null) { holder.itemView.setOnClickListener(onClickListener); + } + if (onLongClickListener != null) { holder.itemView.setOnLongClickListener(onLongClickListener); } if (holder instanceof ImageAndGifEntry.Holder) { - if (!((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.hasOnClickListeners()) { - ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.setOnClickListener(view -> { - if (onClickListener != null && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionStart() == -1 && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionEnd() == -1) { - onClickListener.onClick(view); - } - }); - ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.setOnLongClickListener(view -> { - if (onLongClickListener != null && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionStart() == -1 && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionEnd() == -1) { - return onLongClickListener.onLongClick(view); - } - return false; - }); - } + ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.setOnClickListener(view -> { + if (onClickListener != null + && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionStart() == -1 + && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionEnd() == -1) { + onClickListener.onClick(view); + } + }); + ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.setOnLongClickListener(view -> { + if (onLongClickListener != null + && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionStart() == -1 + && ((ImageAndGifEntry.Holder) holder).binding.captionTextViewMarkdownImageAndGifBlock.getSelectionEnd() == -1) { + return onLongClickListener.onLongClick(view); + } + return false; + }); } } } -- cgit v1.2.3