如果设置了STYLE_NO_TITLE,则使用AppCompatDialog的DialogFragment会崩溃

时间:2015-05-19 15:30:59

标签: android android-support-library android-appcompat android-dialogfragment appcompat-v7-r22.1

我正在尝试在AppCompat v22.1中使用带有片段的新材料主题对话框。 According to Chris Banes,要做到这一点:

  

只需退回new AppCompatDialog(getActivity(), getTheme())即可   onCreateDialog(Bundle)

设置:

public class MyFragment extends DialogFragment
{
    public MyFragment() { }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AppCompatDialog(getActivity(), getTheme());
    }

    ...
}

在正常情况下完美运作;对话框是正确的主题和一切。但是,当我们尝试使用STYLE_NO_TITLE选项显示对话框时:

 MyFragment fragment = new MyFragment();
 fragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
 fragment.show(getSupportFragmentManager(), "DIALOG");

它会导致以下异常并崩溃:

05-19 12:18:38.806  15458-15458/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.test.testdialog, PID: 15458
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)

我猜问题可能与DialogFragment调用requestWindowFeature()而不是supportRequestWindowFeature()(?)

有关

此问题是否有解决方法?

2 个答案:

答案 0 :(得分:3)

这是AppCompat v22.1中的一个错误fixed in v22.1.1

它在第23章和was fixed once more, in v23.0.1再次爆发。

截至撰写本文时,它的工作正常。

答案 1 :(得分:2)

根据Blackbelt链接的问题,我们应该返回AppCompatDialog并使用supportRequestWindowFeature()。我提出这样的改变:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AppCompatDialog dialog = new AppCompatDialog(getActivity(), getTheme());
    return dialog;
}

@Override
public void setupDialog(Dialog dialog, int style) {
    switch (style) {
        case STYLE_NO_INPUT:
            dialog.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            // fall through...
        case STYLE_NO_FRAME:
        case STYLE_NO_TITLE:
            ((AppCompatDialog) dialog).supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }
}

或更好,尽量不要触摸原始代码:

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog,style);
    if(style == STYLE_NO_TITLE) 
         ((AppCompatDialog) dialog).supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}