相关文章推荐
眼睛小的鞭炮  ·  Windows 下 Nginx 配置 ...·  7 月前    · 
愤怒的菠萝  ·  Amazon ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

DayFragment.java (this is a fragment)

final EventPopup eventpopup = new EventPopup(getContext(), readevent);
            eventpopup.show();
            eventpopup.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    if(eventpopup.getDelete()) {
                        ((MainActivity) getActivity()).refreshViewPager();
                    } else if (eventpopup.getEdit()) {
                        ((MainActivity) getActivity()).doEditEvent(readevent, eventpopup.getEditAll());

MainActivity.java:

public void refreshViewPager() {
    doubleViewPager.getAdapter().notifyDataSetChanged();

Why this line gives sometimes NullPointerException on some devices (on others not)?:

                    ((MainActivity) getActivity()).refreshViewPager();

The error message is:

java.lang.NullPointerException:

at de.ubik.terminkalender.DayFragment$8$1.onDismiss (DayFragment.java:1378)

at android.app.Dialog$ListenersHandler.handleMessage (Dialog.java:1749)

at android.os.Handler.dispatchMessage (Handler.java:102)

at android.os.Looper.loop (Looper.java:154)

at android.app.ActivityThread.main (ActivityThread.java:6692)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1468)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1358)

I've got the following structure: MainActivity (with viewpager) -> DayFragment (as a Fragment) -> Dialog. When I close the dialog I want to check if there is a button clicked in the dialog. When clicked, then refresh viewpager in MainActivity. How to achieve that?

I've got the following structure: MainActivity (with viewpager) -> DayFragment (as a Fragment) -> Dialog. When I close the dialog I want to check if there is a button clicked in the dialog. When clicked, then refresh viewpager in MainActivity. How to achieve that? – ubik Jan 13, 2018 at 9:37 Rather than have a "refreshViewpager" callback, have a "buttonClicked" callback. The dialog will tell the Activity that the button was clicked. Then the Activity will dismiss the dialog and refresh itself. – DeeV Jan 13, 2018 at 15:05

as mentioned by @Deev you will always get a null and the solution for this is that call your Dialog from some of the method which has getActivity as a parameter.

sample code:

 //call this from your fragment 
 showDialog(getActivity());

and in your fragment place this somewhere

public void showDialog(final Activity activity){
    final EventPopup eventpopup = new EventPopup(activity, readevent);
        eventpopup.show();
        eventpopup.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if(eventpopup.getDelete()) {
                    ((MainActivity)activity).refreshViewPager();
                } else if (eventpopup.getEdit()) {
                    ((MainActivity) activity).doEditEvent(readevent, eventpopup.getEditAll());

this will work for sure.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.