2012-12-28 56 views
0

我有调用方法的线程,如果发生任何异常,它将返回到该线程的catch块。如何在线程内发生异常时调用方法

后来,然后我喜欢调用处理程序或吐司消息来显示异常,但我不能称它显示错误。

我该怎么做才能解决问题,任何想法。

在此先感谢。

private void onSaveDialog() {  

    final ProgressDialog dialog = new ProgressDialog(this); 

    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    dialog.setTitle("mApprove Advantage"); 
    dialog.setIcon(R.drawable.mapprove1); 
    dialog.setMessage("Theme setting is in progress..."); 
    dialog.setIndeterminate(false); 
    dialog.setCancelable(false); 
    dialog.show(); 
    new Thread() { 
     @Override 
     public void run() { 
      try { 

       Thread.sleep(5000);  
       synchronized (this) { 
       onsave(); 
      } 
       dialog.cancel(); 
       onSetting(); 
      } catch (Exception e) { 
       dialog.cancel(); 
       wrongurl=true; 
       onFinishDialog1(); 
       System.out.println("url wrong"); 

      } 

     }   
    }.start(); 
    } 

private void onFinishDialog1(){ 
    if(wrongurl){ 
     wrongurl=false; 
     Toast.makeText(getApplicationContext(), 
       "URL not Available.", Toast.LENGTH_LONG) 
       .show(); 
    } 

} 
+0

“但我无法调用它显示错误。” - 为什么? – Simon

+1

发布你的代码到目前为止你所尝试过的是什么? –

+0

我不知道,线程不允许在出错时调用任何方法或处理程序 – saran

回答

0

使用此:

runOnUiThread(new Runnable() 
     {     
      @Override 
      public void run() 
      { 
      try { 

       Thread.sleep(5000);  
       synchronized (this) { 
       onsave(); 
      } 
       dialog.cancel(); 
       onSetting(); 
      } catch (Exception e) { 
       dialog.cancel(); 
       wrongurl=true; 
       onFinishDialog1(); 
       System.out.println("url wrong"); 

      } 
      } 
}); 

感谢。

+0

非常感谢 – saran

0

使用runOnUiThread用于显示从螺纹敬酒消息:

// your code here 
    } catch (Exception e) { 
    Your_Activity.this.runOnUiThread(new Runnable() { 
      public void run() { 
      dialog.cancel(); 
      wrongurl=true; 
      onFinishDialog1(); 
      // show your message here 
      Toast.makeText(Your_Activity.this, "Error", Toast.LENGTH_LONG).show(); 

      } 
    }); 
    } 
相关问题