我正在尝试加载DialogFragment,但有时在onCreateDialog上会得到null。我不确定它何时发生,但不是很罕见。我该如何解决?

实用工具

    currentDialog = PopupDialog.newInstance(type, title, maString, isCancelable);
    currentDialog.show(ft, "dialog");

PopupDialog.java
public class PopupDialog extends DialogFragment{


public static final int POPUP_ERROR = 0;
public static final int POPUP_WARNNING = 1;

private int type;
private String title;
private String messageString;
private boolean isCancelable;

public static PopupDialog newInstance(int type,String title,String maString,boolean isCancelable) {
    PopupDialog f = new PopupDialog();

    Bundle args = new Bundle();
    args.putInt("type",type);
    args.putString("title", title);
    args.putString("maString", maString);
    args.putBoolean("isCancelable", isCancelable);
    f.setArguments(args);

    return f;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle arg = getArguments();
    type = arg.getInt("type");
    title = arg.getString("title");
    messageString = arg.getString("maString");
    isCancelable = arg.getBoolean("isCancelable");
    setStyle(DialogFragment.STYLE_NO_TITLE,0);
    if (!isCancelable){
        setCancelable(false);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.popup_error, container, false);

    TextView popupTitle = (TextView) v.findViewById(R.id.popupTitle);
    popupTitle.setText(title);

    TextView popupMessage = (TextView) v.findViewById(R.id.popupMessage);
    popupMessage.setText(messageString);

    ImageView popupIcon = (ImageView) v.findViewById(R.id.popupIcon);
    messageString = messageString + "\n";
    if(type == POPUP_ERROR){
        messageString = messageString + getString(R.string.error_default_ext);
        popupIcon.setImageResource(R.drawable.icon_error);
    }
    else{
        popupIcon.setImageResource(R.drawable.warning_icon);
    }

    popupMessage.setText(messageString);


    Button okButton = (Button) v.findViewById(R.id.okButton);

    okButton.setOnClickListener(new dismissDialogOnClick());

    if (!isCancelable){
        okButton.setVisibility(View.GONE);
    }
    return v;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog d = super.onCreateDialog(savedInstanceState);
    d.setCanceledOnTouchOutside(false);
    return d;
 }

编辑:

附加了堆栈跟踪。如果我尝试在getDialog onCreateDialog上进行操作(像第一种情况一样,它不会总是发生
java.lang.NullPointerException
    at android.app.DialogFragment.onActivityCreated(DialogFragment.java:469)
    at android.app.Fragment.performActivityCreated(Fragment.java:1703)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
    at android.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

最佳答案

getDialog().setCanceledOnTouchOutside(false);

调用内部show之后,getDialog返回Dialog对象。在onCreateView中调用它还为时过早。您可以重写onCreateDialog,检索超类返回的对象,然后在此对象上调用方法
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog d = super.onCreateDialog(savedInstanceState);
    d.setCanceledOnTouchOutside(false);
    return d;
 }

08-18 17:39