2010-08-18 48 views
2

我正在LG前夕手机上测试我的应用程序。我有一个应用程序试图从网上下载一些东西,当它抛出一个异常时,它应该启动一个alertdialog,说有错误。当手机没有wifi信号时,程序在builder.create()中崩溃(见下面的代码)。然而,当有wifi信号,并且其他异常(例如,url中的输入错误)引发异常时,对话框会按照它应有的方式启动。任何线索,为什么这可能是?应用程序崩溃时做AlertDialog.Builder create()方法 - 安卓

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

代码的AsyncTask调用的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

你可以显示异常的logcat/full stacktrace吗? – 2010-08-18 22:29:48

回答

6

不要在doInBackground显示对话框。该方法不会在UI线程上运行。尝试在onPostExecute或onProgressUpdate上显示错误对话框。

+0

谢谢!现在效果很好 – meow 2010-08-20 16:19:37