Android 删除Alertdialog,以便newAlertDialog()将调用onCreateDialog

Android 删除Alertdialog,以便newAlertDialog()将调用onCreateDialog,android,dialog,android-alertdialog,Android,Dialog,Android Alertdialog,我遇到了只调用一次onCreateDialog的问题。这对我来说是个问题,因为我在该方法中引用了xml布局,每当我关闭对话框并第二次调用它时,这些引用就消失了。代码如下: if (position == 0){ PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance(); playerNameDialog.show(getFragmentManager(), TeamAct

我遇到了只调用一次onCreateDialog的问题。这对我来说是个问题,因为我在该方法中引用了xml布局,每当我关闭对话框并第二次调用它时,这些引用就消失了。代码如下:

        if (position == 0){

        PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance();
        playerNameDialog.show(getFragmentManager(), TeamActivity.PLAYER_DIALOG);

    }
    else {
        String playerName = teamPlayers.get(position).toString();
        Player selectedPlayer = team.FindPlayer(playerName);
        PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance();



        playerNameDialog.changePlayerName(selectedPlayer.playerName);
        playerNameDialog.changePlayerRating(selectedPlayer.playerRating);
        playerNameDialog.show(getFragmentManager(), TeamActivity.PLAYER_DIALOG);

    }
和我的警报对话框

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreateDialog(savedInstanceState);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    view = inflater.inflate(R.layout.player_name_dialog, null);

    playerRatingSeekBar = (SeekBar) view.findViewById(R.id.player_rating_sbar);
    playerRatingSeekBar.setOnSeekBarChangeListener(this);
    playerRatingTxtView = (TextView) view.findViewById(R.id.player_rating);
    playerRatingTxtView.setText(getString(R.string.player_rating)+" "+playerRatingSeekBar.getProgress());
    playerNameEditText = (EditText) view.findViewById(R.id.player_name);

    builder = new AlertDialog.Builder(getActivity());

    builder.setView(view);



    builder.setMessage("New Player");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {

            mListner.onPNDialogPositiveClick(PlayerNameDialog.this, view);

        }

    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mListner.onPNDialogNegativeClick(PlayerNameDialog.this, view);

        }
    });

    return builder.create();

}
第二次创建新对话框实例时(在else语句下),不再调用onCreateDialog方法。因此,所有edittext引用都消失了。我尝试使用dimis()和removefragment方法,但没有成功

怎么解决这个问题


提前感谢。

是的,创建另一个您应该创建的对话框类,而不是尝试手动调用
onCreateDialog
,您永远不应该感谢您的帮助,但我使用的是dialogFragment而不是Dialog类。。。
try this:
 private static AlertDialog dialog;
public static void showDialog(Context context, String message) {
    if (context != null) {
        TextView textMsg;
        TextView textOk;
        LinearLayout textOKLayout;

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = layoutInflator.inflate(R.layout.alert_dialog, null);
        alertDialog.setView(dialogView);
        textMsg = (TextView) dialogView.findViewById(R.id.text);
        textOk = (TextView) dialogView.findViewById(R.id.textOk);
        textOKLayout = (LinearLayout) dialogView.findViewById(R.id.textOKLayout);
        textMsg.setText(message);
        textOKLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });

        dialog = alertDialog.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.show();
    } else {
        return;
    }

}