2010-08-18 16 views
2

Je teste ma demande sur un téléphone LG Eve. J'ai une application qui essaie de télécharger quelque chose sur le web, et quand il lance une exception, il est censé lancer un alertdialog disant qu'il y avait une erreur. Lorsque le téléphone n'a pas de signal wifi, le programme se bloque à builder.create() (voir le code ci-dessous). Cependant, quand il y a un signal wifi, et que l'exception est lancée par quelque chose d'autre (par exemple, une faute de frappe dans l'url), la boîte de dialogue se lance comme elle le devrait. Toute idée de pourquoi cela pourrait être?application se bloque tout en faisant méthode AlertDialog.Builder créer() - Android

Code pour onCreateDialog:

@Override 
protected Dialog onCreateDialog(int id){ 

    Dialog d = null; 
    switch (id){ 

    case DIALOG_DATA_ERROR_ID: 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage(getResources().getString(R.string.error_data)); 
     builder.setCancelable(false); 
     builder.setNeutralButton("OK", new DialogInterface.OnClickListener(){ 

      public void onClick(DialogInterface d, int id){ 

       d.cancel(); 
      } 
     }); 
     d = builder.create(); 
     break;  
    } 
    return d; 
} 

Code pour AsyncTask qui appelle showDialog:

private static class DownloadJSONTask extends AsyncTask<String, Void, String>{ 


    private ProgressDialog dialog; 
    private Activity parent; 
    private JSONParserInterface jsonParser; 

    public DownloadJSONTask(Activity parent, JSONParserInterface jsonParser){ 
     this.parent = parent; 
     this.jsonParser = jsonParser; 
    } 

     protected void onPreExecute(){ 

      dialog = ProgressDialog.show(parent, "Loading", "Please Wait...", true); 

     } 

     protected String doInBackground (String... urls){    

      try { 

      return HttpHelper.getResponse(urls[0]); 

      }catch (Exception e){ 
       dialog.cancel(); 
       parent.showDialog(BoomSetListActivity.DIALOG_DATA_ERROR_ID); 
      } 

      return null; 

     } 

     protected void onPostExecute(String json){ 
      dialog.cancel(); 
      if (jsonParser != null) jsonParser.parse(json); 
     } 


} 
+0

Pouvez-vous montrer le logcat/plein stacktrace pour les exceptions? –

Répondre

6

Ne pas afficher de dialogue à doInBackground. La méthode ne s'exécute pas sur le thread d'interface utilisateur. Essayez d'afficher la boîte de dialogue d'erreur à l'adresse onPostExecute ou onProgressUpdate.

+0

merci! fonctionne très bien maintenant – meow