创建DialogFragment时报错

在创建DialogFragment 设置屏幕属性时会报错

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    setStyle(STYLE_NO_FRAME, 0); //此处报如下错误
    dialog.setContentView(R.layout.common_dialog_fragment);
    return dialog;
}


 android.util.AndroidRuntimeException: requestFeature() must be called before adding content
            at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
            at android.app.Dialog.requestWindowFeature(Dialog.java:1066)
            at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:317)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


解决方案之一是在创建Dialog时指定sytle的方法,也可以在

dialog.setContentView(R.layout.common_dialog_fragment);之前调用window属性。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(getActivity(), R.style.dialog_fragment);
    dialog.setContentView(R.layout.common_dialog_fragment);
    return dialog;
}

style定义如下:


    @null   
    false  
    true  
    @android:color/transparent< !--设置背景为透明-->


转载于:https://my.oschina.net/u/2317656/blog/412396

你可能感兴趣的:(创建DialogFragment时报错)