Bruse
Bruse

Reputation: 313

Dialogfragment onCreateDialog NullPointerException

I'm trying to load DialogFragment, but I sometimes get null on onCreateDialog. I'm not sure when it's happening, but not very rarely. How can I solve this?

Utils.java

    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; 
 }

EDIT:

Attached the stack trace. If i'm trying to do getDialog onCreateDialog (like first case, it doesn't happen always

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)

Upvotes: 4

Views: 1358

Answers (2)

Blackbelt
Blackbelt

Reputation: 157487

getDialog().setCanceledOnTouchOutside(false);

getDialog returns the Dialog object after the internal show is called. Calling it in onCreateView is too early. You can override onCreateDialog, retrive the object returned by the super class, and call the method on this object

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

Upvotes: 4

greywolf82
greywolf82

Reputation: 22193

You can call getDialog() only when the dialog is shown. You could register an "on shown listener" on your alert dialog to get a reference of the dialog.

Upvotes: 0

Related Questions