2011-04-17 5 views
1

créer et montrer ma boîte de dialogue de manière suivante:AlertDialog dans 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; 
       } 

En 2.2 Il fonctionne très bien, mais en 2.1 il provoque la force étroite avec -

« java.lang .Illegalargumentexeption: Activité onCreateDialog n'a pas créé une boîte de dialogue pour id 1"

Pourquoi donc?

Répondre

1

Si remplacer

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

sur

return builder.create(); 

Il commence à travailler comme prévu. Ne sais pas pourquoi.

+0

J'ai fixé voir http://stackoverflow.com/questions/6259644/android-java-lang-illegalargumentexception-erron-when-creating-dialog-on-2-1-a/13938677#13938677 – powder366

0

Je suppose qu'il est à cause de this

protected Dialog onCreateDialog (int id) 

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

si cette ligne

Protected Dialog onCreateDialog(int id) { 

devrait être quelque chose comme ça (non testé, mais assez sûr)

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

public final booléen showDialog (int id, Ensemble d'arguments) Depuis: API Niveau 8. Mais je veux l'utiliser dans l'API 7. Et je ne comprends pas ce que je suis censé passer à travers le bundle? – Divers