将对话框转换为AlertDialog

时间:2018-05-05 16:06:20

标签: android dialog alertdialog

我在antoher项目中有完全相同的代码,但它在这里继续崩溃。我的格斗里有vvec.push_back(v)。我真的不确定问题是什么。我已尝试切换gradle版本,从implementation 'com.android.support:design:26.1.0'更改为v7 Dialog。无论

,所有都失败了
app Dialog
java.lang.ClassCastException: android.app.Dialog cannot be cast to android.support.v7.app.AlertDialog
                      at com.example.weather.CreateCityDialog.onResume(CreateCityDialog.java:58)

1 个答案:

答案 0 :(得分:2)

您必须从AlertDialog返回onCreateDialog()

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Add new city");

    View rootView = getActivity().getLayoutInflater().inflate(R.layout.activity_create_city_dialog, null);

    etName = rootView.findViewById(R.id.etName);

    builder.setView(rootView);

    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    return builder.create();
}