0

我无法处理由方法调用的异常。我要做的是在捕获到异常时创建一个警报对话框(我知道如何创建警报对话框)。抛出异常的方法在不同的类中,这就是为什么在捕获异常时无法创建警报对话框的原因。见下文:-

protected Boolean doInBackground(final String... args) { 

    try{
        ParserLive parser = new ParserLive();
        feeds = parser.parse(); // this is the method throwing the exception
        return true;  //won't return true because it gets stuck here
    } catch (Throwable t){
        return false;
    }    
} 

下面是方法所在的 ParserLive 类:-

public class ParserLive {

       //variables and constructor
       //Below is the method I want to handle

       //Ideally I'd like to wrap the code inside this method with a try-catch, 
       //and put the dialog in the catch statement, but this is not allowed.
       public List<Feed> parse() {
       //some code 
       // the following code is throwing the error, when I try to create an alert dialog inside this catch statement it says "the constructor AlertDialog.Builder(ParserLive) is undefined"
       try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
       } catch (Exception e) {
             throw new RuntimeException(e); 
         }
     return feeds;
  }
}

编辑

我已经编辑了上面的代码以包含在 LogCat 中引发以下错误的代码行 - “ java.lang.RuntimeException: org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found ”

4

1 回答 1

0
        protected Boolean doInBackground(final String... args) { 
            try{
                ParserLive parser = new ParserLive();
                feeds = parser.parse(); 
                return true;  
            } catch (Exception e){
                log.d("Error", e.getMessage());
                yourActivity.runOnUiThread(new Runnable() {
                                  public void run() {
                                      Toast.makeText(context, e.getMessage(), 3000).show();        }
                                 });
//Here you can create dialog also
                return false;
            }    
        }
于 2012-04-15T17:22:42.873 回答