简体   繁体   English

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

[英]Android: force a thread to close and display an alert dialog

I have a thread inside which I catch an exception. 我有一个线程在其中捕获异常。 What I want is that when this happens, the thread closes/quits/dies or whatever I should say, and an alert dialog is displayed (no like toasts!). 我想要的是,当发生这种情况时,线程关闭/退出/死亡或我应该说的任何内容,并显示一个警报对话框(不像敬酒!)。

Here's my code: 这是我的代码:

    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
}

This code crashes, I think it has something to do with t1.interrupt (tried stop instead, but didn't work either). 这段代码崩溃了,我认为它与t1.interrupt有关(尝试停止,但也没有用)。

How can I fix this code? 如何解决此代码? Thank you in advance. 先感谢您。

Why do you have the t1.interrupt() anyway? 为什么仍然有t1.interrupt()? Seems like your thread will be terminated regardless so what is the reason for putting it there? 似乎您的线程将被终止,所以将其放置在那里的原因是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM