2011-04-17 78 views
1

我创建并显示我的对话在明年方式:AlertDialog在Android 2.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: 活动#onCreateDialog没有创造 为ID 1" 的对话

为什么会这样?

回答

1

如果

return builder.create(); 

更换

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

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

+0

I fixed see http://stackoverflow.com/questions/6259644/android-java-lang-illegalargumentexception-erron-when-creating-dialog-on-2-1-a/13938677#13938677 – powder366 2012-12-18 17:54:12

0

我猜想那是因为this

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) { 
+0

public final boolean showDialog(int id,Bundle args) 从以下版本开始:API Level 8。但我想在API 7中使用它。我不明白我应该通过这个包来通过什么。 – Divers 2011-04-17 09:54:27