如何在 activity 中显示自定义的 DialogFragment

How to show a customized DialogFragment in the activity

我正在尝试构建一个 AlertDialog 并希望在主 activity 启动时显示它。但是当 activity 启动时,出现错误:

android.util.AndroidRuntimeException: Window feature must be requested before adding content

我首先要做的是创建一个 AlertFragment:

public class AlertFragment extends DialogFragment{

    public static AlertFragment newInstance(){
        return new AlertFragment();
    }

    public AlertFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState){

        super.onCreateView(inflater, container, SavedInstanceState);

        return inflater.inflate(R.layout.alert_dialog, container, false);
    }

    @Override
    public Dialog onCreateDialog(Bundle SaveInstanceState){

        return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
            }
        }).create();
    }
}

以下为主要activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertFragment AF = AlertFragment.newInstance();
        AF.show(getSupportFragmentManager(), "Dialog");
    }
}

谁能告诉我问题出在哪里?

在您的 AlertFragment 中删除 oncreateview 和 运行

public class AlertFragment extends DialogFragment{

public static AlertFragment newInstance(){
    return new AlertFragment(); }

public AlertFragment(){}



@Override 
public Dialog onCreateDialog(Bundle SaveInstanceState){

    return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
        }
    }).create(); 
} 

}

只需在您的代码中使用 show 代替 create,如下所示:

public Dialog onCreateDialog(Bundle SaveInstanceState){ 

return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
    }
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
    }
}).show();