Android自定义对话框实例化

Android自定义对话框实例化,android,dialog,fragment,Android,Dialog,Fragment,我正在尝试学习对话框 从androids官方文档教程中,我创建了自己的自定义对话框: public class LoginDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get

我正在尝试学习
对话框

从androids官方文档教程中,我创建了自己的自定义对话框:

public class LoginDialog extends DialogFragment {

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.login, null))
           // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                // sign in the user ...
              }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 LoginDialog.this.getDialog().cancel();
               }
            });
    return builder.create();
  }
}
现在我想从我的活动中调用它

因此,我做了以下工作:
(来自我的登录活动)

但是,我得到以下错误:

prev cannot be applied to android.app.fragment
我不确定我做错了什么,也不确定我应该从这里走到哪里。

因为:

prev无法应用于android.app.fragment

因为LoginDialog类不包含任何以Fragment为参数并返回LoginDialog实例的构造函数。如果您使用的是Android,则会忘记返回LoginDialog Fragment实例的
newInstance
方法。您应该以相同的方式或尝试:

FragmentManager fm = getFragmentManager();
LoginDialog fragment = new LoginDialog();
fragment.show(fm, "dialog");

只需执行此对话框fragment=new LoginDialog();对于show fragemnt.show()@Hardik当我这样做的时候,它会给我一个错误,说它缺少一个片段。这里是一个完整的例子==>这会给我以下在fragment.show(fm,“dialog”)的错误;无法解析方法show(android.app.FragmentManger,java.lang.string)@MarcRasmussen:请参见此处[DialogFragment中提供的两种方法尝试以相同的方式进行解析]
FragmentManager fm = getFragmentManager();
LoginDialog fragment = new LoginDialog();
fragment.show(fm, "dialog");