Android:强制线程关闭并显示警告对话框

时间:2012-12-15 00:19:59

标签: android multithreading dialog alert

我有一个线程,我在其中捕获异常。我想要的是,当这种情况发生时,线程关闭/退出/死亡或我应该说什么,并显示一个警告对话框(不喜欢祝酒!)。

这是我的代码:

    t1 = new Thread(new Runnable()
    {   
        public void run() 
        {
            Looper.prepare();
            StringBuffer stringBuffer = new StringBuffer("");
            BufferedReader bufferedReader = null;
            URI uri = null;

            try
            {
                requestAndMakeSheet(stringBuffer, bufferedReader, uri);
            }
            catch (Exception e)
            {
                //Toast.makeText(getBaseContext(), "Web Request Error", Toast.LENGTH_LONG).show();
                //Log.e("Web Request Error", e.getMessage());
                t1.interrupt();
                AlertDialog.Builder parsingErrorBox = new AlertDialog.Builder(ReservationInfo.this);
                parsingErrorBox.setTitle("Login error");
                parsingErrorBox.setMessage("You may have to check your credentials and then try again.");
                parsingErrorBox.show();
            }
            finally
            {
                if (bufferedReader!=null)
                {
                    try
                    {
                        bufferedReader.close();
                    }
                    catch (IOException ioe)
                    {
                        Log.e("Web Request Error", ioe.getMessage());
                    }
                }
            }
        }           
    });
    t1.start();
    try 
    {
        t1.join();
        mWebview.loadUrl("file:///"+Environment.getExternalStorageDirectory()+"/MySheet.html");
        setContentView(mWebview);
    } 
    catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //End of le thread
}

此代码崩溃,我认为它与t1.interrupt有关(尝试停止,但也没有工作)。

如何修复此代码? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

为什么你还有t1.interrupt()?似乎你的线程将被终止,无论如何将它放在那里是什么原因?