0

我有一个调用方法的线程,如果发生任何异常,它会返回到该线程的 catch 块。

稍后,我喜欢调用处理程序或 toast 消息来显示异常,但我无法调用它来显示错误。

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

提前致谢。

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();
    }

}
4

2 回答 2

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");

            }
          }
 });

谢谢。

于 2012-12-28T09:56:57.670 回答
0

使用runOnUiThread将来自 Thread 的 toast 消息显示为:

   // 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(); 

          }
     });
   }
于 2012-12-28T09:55:09.353 回答