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 に答える