为什么这个AlertDialog没有显示?

时间:2015-03-12 08:15:55

标签: android

我创建了AlertDialog

public class MessageDialogView extends AlertDialog {

    private Context ctxt;
    private View contenu, titleBar;

    @SuppressLint("NewApi")
    public MessageDialogView(Context context, LayoutInflater inflater) {
        super(context);
        ctxt = context;
        contenu = inflater.inflate(R.layout.msg_dialog, null);
        titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCustomTitle(titleBar);
        setView(contenu, 0, 0, 0, 0);
        setButton(DialogInterface.BUTTON_POSITIVE, ctxt.getResources().getString(R.string.button_ok), new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    }
    public void setTitre(String titre) {
        if (titre != null)
            ((TextView)titleBar.findViewById(R.id.titre)).setText(titre);
    }
    public void setMsg(String text){
        if (text != null)
            ((TextView)contenu.findViewById(R.id.msgText)).setText(text);
    }

}

xml layout非常简单(不需要在这里复制代码:))

当我尝试显示AlertDialog时,没有显示任何内容:只是屏幕变暗了!

public class SyncActivity extends Activity {

    private RadioButton webVersMobile = null;
    private MessageDialogView dlg = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.syncro);
        webVersMobile = (RadioButton) findViewById(R.id.webMobile);
        dlg = new MessageDialogView(SyncActivity.this, getLayoutInflater());
    }
    ...
    public void displayError(String msg) {
        dlg.setTitre(getString(R.string.titreErrMsgBox));
        dlg.setMsg(msg);
        dlg.show();
    }
    ...
}

我将对话框称为:

private class RequestTask extends AsyncTask<String, Void, String> { 

        ...

        @Override
        protected String doInBackground(String... s_url) {
            ...
        }

        @Override
        protected void onPostExecute(String result) {
            if (error) {
                displayError(result);
            } else {

            }
        }

        private void displayError(String msg) {
            dlg.setTitre(getString(R.string.titreErrMsgBox));
            dlg.setMsg(msg);
            dlg.show();
        }
    }

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:0)

您忘记调用show()方法来显示对话框。

dlg = new MessageDialogView(SyncActivity.this, getLayoutInflater());

在此行之后,在dlg.show();方法中写下onCreate()

答案 1 :(得分:0)

好的,我发现我的错误原因是我实现了onCreate方法。当我删除实现时,会显示对话框:)

public class MessageDialogView extends AlertDialog {

    private View contenu, titleBar;

    @SuppressLint("InlinedApi")
    public MessageDialogView(Context context, LayoutInflater inflater) {
        super(context, AlertDialog.THEME_HOLO_DARK);
        contenu = inflater.inflate(R.layout.msg_dialog, null);
        titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
        setCustomTitle(titleBar);
        setView(contenu, 0, 0, 0, 0);
        setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_ok), new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    }
    public void setTitre(String titre) {
        if (titre != null)
            ((TextView)titleBar.findViewById(R.id.titre)).setText(titre);
    }
    public void setMsg(String text){
        if (text != null)
            ((TextView)contenu.findViewById(R.id.msgText)).setText(text);
    }

}