views:

97

answers:

1

Hi.

I am testing my application on an LG Eve phone. I have an application that tries to download something from the web, and when it throws an exception, it is supposed to launch an alertdialog saying that there was an error. When the phone has no wifi signal, the program crashes at builder.create() (see code below). However, when there is wifi signal, and the exception is thrown by something else (for instance, a typo in the url), the dialog launches the way it's supposed to. Any clue as to why this could be?

Code for 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 for AsyncTask that calls 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);
       }


}
+2  A: 

Do not show dialog at doInBackground. The method doesn't run on UI thread. Try showing error dialog at onPostExecute or onProgressUpdate.

Konstantin Burov
thanks! works great now
meow