开发者

AlertDialog in android 2.1

开发者 https://www.devze.com 2023-02-27 01:50 出处:网络
I create and show my dialog i开发者_StackOverflown next way: showDialog(1); // Logcat say me that mistake is here.

I create and show my dialog i开发者_StackOverflown next way:

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;
                }

In 2.2 It works very well, but in 2.1 it causes force close with -

"java.lang.Illegalargumentexeption: Activity#onCreateDialog did not create a dialog for id 1"

Why so?


If replace

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

on

return builder.create();

It starts work as expected. Dont know why.


I'm assuming it is because of this

protected Dialog onCreateDialog (int id)

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

so this line

 Protected Dialog onCreateDialog(int id) {

Should be something like this (untested, but pretty sure)

 Protected Dialog onCreateDialog(int id, Bundle yourBundle) {
0

精彩评论

暂无评论...
验证码 换一张
取 消