gpt4 book ai didi

java - 为什么 ((MainActivity) getActivity()) 给我一个 NullPointerException?

转载 作者:行者123 更新时间:2023-12-02 02:22:55 25 4
gpt4 key购买 nike

你好,

我有以下代码:

EventPopup.java

class EventPopup extends Dialog {

private boolean delete;
private boolean edit;
private boolean editall;

private ReadEvent event;

... }

DayFragment.java(这是一个 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();
}

为什么此行有时会在某些设备上出现 NullPointerException(在其他设备上则不会)?:

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

错误信息是:

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)

我有以下结构:MainActivity(带有viewpager)-> DayFragment(作为 fragment )-> Dialog。当我关闭对话框时,我想检查对话框中是否单击了按钮。点击后,然后刷新MainActivity中的viewpager。如何实现这一目标?

最佳答案

onDismiss

这意味着您的对话框已关闭,它与您的 Activity 分离,这意味着 getActivity() 将返回 null。最好找到另一种方法来刷新viewpager。如何执行此操作取决于您刷新的原因。

关于java - 为什么 ((MainActivity) getActivity()) 给我一个 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48232840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com