我正在尝试学习Dialogs
在关于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();
}
}
现在我希望从我的活动中调用它。
所以我做了以下事情:(from my Login activity)
FragmentTransaction ft = getFragmentManager().beginTransaction();
android.app.Fragment prev = getFragmentManager().findFragmentByTag("dialog");
DialogFragment fragment = new LoginDialog(prev);
然而我收到以下错误:
prev cannot be applied to android.app.fragment
我不确定我做错了什么或者应该从哪里开始。
答案 0 :(得分:1)
获得:
prev不能应用于android.app.fragment
因为LoginDialog类不包含任何将Fragment作为参数并返回LoginDialog实例的构造函数。如果您使用的是Android DialogFragment Example,则忘记返回LoginDialog DialogFragment实例的newInstance
方法。你应该以同样的方式做或尝试:
FragmentManager fm = getFragmentManager();
LoginDialog fragment = new LoginDialog();
fragment.show(fm, "dialog");