2015-02-08 76 views
3

我想加載DialogFragment,但我有時在onCreateDialog上得到空值。我不確定它何時發生,但不是很少。我該如何解決這個問題?Dialogfragment onCreateDialog NullPointerException

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) 

回答

3
getDialog().setCanceledOnTouchOutside(false); 

getDialog返回內部放映後Dialog對象被調用。在onCreateView調用爲時尚早。您可以覆蓋onCreateDialog,retrive通過超類返回的對象,並調用該方法這個對象上

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

現在我在創建函數的DialogFragment類上得到了null異常。我想這太容易了:) – Bruse 2015-02-08 16:53:00

+0

它是不相關的。你能發佈堆棧跟蹤嗎? – Blackbelt 2015-02-08 16:54:54

+0

我在第一條消息上發佈了堆棧 – Bruse 2015-02-09 06:52:47

0

你可以叫getDialog()中所示的對話框,只有當你可以註冊一個「上顯示監聽器」在您的警報對話框中獲取d的參考ialog。