我在antoher项目中有完全相同的代码,但它在这里继续崩溃。我毕业了。我真的不确定问题是什么。我试着切换梯度版本,从implementation 'com.android.support:design:26.1.0'切换到v7 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)

import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v4.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CreateCityDialog extends DialogFragment {
    public interface NewCityHandler {
        void onNewCityCreated(String cityName);
    }

    private NewCityHandler newCityHandler;
    private EditText etName;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof NewCityHandler)
            newCityHandler = (NewCityHandler) context;
        else
            throw new RuntimeException("Error");
    }

    @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 super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        final AlertDialog d = (AlertDialog) getDialog();
        if (d != null) {
            Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(etName.getText())) {
                        newCityHandler.onNewCityCreated(etName.getText().toString());
                        d.dismiss();
                    } else {
                        etName.setError("Empty field");
                    }
                }
            });
        }
    }
}

最佳答案

你必须从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();
}

10-08 03:28