0

I cannot figure out how to assign onGifSelected while using Java. https://developers.giphy.com/docs/sdk#android I've created a button which shows the Giphy dialog correctly, but nothing happens when I choose a gif (I have to implement the function).

gifBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GiphyDialogFragment.Companion.newInstance(gphSettings).show(getSupportFragmentManager(), "this");

                gifSelectionListener.onGifSelected(media);
                gifSelectionListener.onDismissed();

        }
    });

I get the following error, even though I have declared "media" in the beginning of the class.

java.lang.NullPointerException: Attempt to invoke interface method 'void com.giphy.sdk.ui.views.GiphyDialogFragment$GifSelectionListener.onGifSelected(com.giphy.sdk.core.models.Media)' on a null object reference
    at com.example.alexevangelou.bumpr.MessageActivity$5.onClick(MessageActivity.java:196)

1 Answers1

2

Initialize the api key before showing the dialog fragment:

    Giphy.INSTANCE.configure(getActivity(), your_key, false);       
    GPHSettings gphSettings = new GPHSettings();

        GiphyDialogFragment gdl = GiphyDialogFragment.Companion.newInstance(gphSettings);

        gdl.setGifSelectionListener(new GiphyDialogFragment.GifSelectionListener() {
            @Override
            public void onGifSelected(@NotNull Media media) {

                Image image = media.getImages().getFixedWidth();
                String gif_url = image.getGifUrl();
            }

            @Override
            public void onDismissed() {
            }
        });

        gdl.show(getActivity().getSupportFragmentManager(), "this");

Also Works in fragment context.

ankalagba
  • 94
  • 1
  • 8
  • 1
    please provide some explanation for your answer. you can refer to [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Saeed Zhiany Oct 21 '19 at 11:52