Dialogfragment onCreateDialog NullPointerException

时间:2015-02-08 16:31:04

标签: java android dialog android-dialogfragment

我试图加载DialogFragment,但我有时会在onCreateDialog上获取null。我不确定它什么时候发生,但不是很少。我怎么解决这个问题?

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

编辑:

附上堆栈跟踪。如果我想尝试使用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)

2 个答案:

答案 0 :(得分:4)

getDialog().setCanceledOnTouchOutside(false);

getDialog在调用内部节目后返回Dialog对象。在onCreateView中调用它太早了。您可以覆盖onCreateDialog,检索超类返回的对象,并在此对象上调用该方法

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

答案 1 :(得分:0)

只有在显示对话框时才能调用getDialog()。您可以在警报对话框中注册“在显示的侦听器上”以获取对话框的引用。