简体   繁体   中英

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).

How can I fix this code? Thank you in advance.

Why do you have the t1.interrupt() anyway? Seems like your thread will be terminated regardless so what is the reason for putting it there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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