1

我以以下方式创建并显示我的对话框:

showDialog(1); // Logcat say me that mistake is here.
protected Dialog onCreateDialog(int id) {
            switch (id) {
            case 1:{
                Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.SelectLoc)
                        .setCancelable(true)
                        .setPositiveButton(R.string.Phone, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        if (mExternalStorageAvailable)
                                        {
                                        PathOpenFile = Environment.getExternalStorageDirectory().getPath();
                                        FileManagerActivity(Settings.Pref.getString("Path_Open", PathOpenFile), REQUEST_LOAD);
                                        }
                                        else 
                                            Toast.makeText(Main.this, R.string.CheckSD , Toast.LENGTH_LONG).show();
                                    }
                                })
                        .setNegativeButton(R.string.Ftp, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int which){  
                                    if (Settings.Pref.getBoolean("Ftp_User",false))
                                    {
                                        FtpConnect _FtpConnect = new FtpConnect();
                                        _FtpConnect.Save_Open = FTP_REQUEST_LOAD;
                                        _FtpConnect.execute();
                                    }
                                else 
                                Toast.makeText(Main.this, R.string.SetPass , Toast.LENGTH_LONG).show();
                                    }
                                });
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
                }

在 2.2 中它工作得很好,但在 2.1 中它会导致强制关闭 -

“java.lang.Illegalargumentexeption:Activity#onCreateDialog 没有为 id 1 创建对话框”

为什么这样?

4

2 回答 2

1

如果更换

 AlertDialog dialog = builder.create();
    dialog.show();
    break;

return builder.create();

它按预期开始工作。不知道为什么。

于 2011-04-17T17:48:26.360 回答
0

我假设是因为这个

protected Dialog onCreateDialog (int id)

Since: API Level 1
This method is deprecated.
Old no-arguments version of onCreateDialog(int, Bundle).

所以这条线

 Protected Dialog onCreateDialog(int id) {

应该是这样的(未经测试,但很确定)

 Protected Dialog onCreateDialog(int id, Bundle yourBundle) {
于 2011-04-17T09:38:31.593 回答