tags:

views:

49

answers:

2
progressDialog = ProgressDialog.show(GetResponse.this, "", "Loading...");

new Thread() 
        {
            public void run() 
            {
                try
                {
// inside i have written code for making connection to the server using SSL connection.
}catch (Exception e)

  {     
            progressDialog.dismiss();     
             exception(e.getMessage())
                  }.start(); 
 }
 private void exception(String msg) 
 {
  Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  this.finish();
  Intent i = new Intent(getBaseContext(), LoginPage.class);
  startActivity(i);

 }

my LoginPage.java is previous activity. If the connection is successfull it goes to the next activity ot doesnt give any error, But if der is any prob with connection then i want progress bar should be stopped and go back to the LoginPage activity and also i want the error msg to be displayed. From the above im getting some error.. Please help me out on this

A: 

Pass in and use the context from LoginPage. Also, use the 101010 button to format your code as code in your posts.

JackN
A: 

hi, you can go up by using try catch mechanism where in your catch place your toast message and u can do it also by asynchronous task, here simple code

private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(
                Bru_Sports_View.this);

        // can use UI thread here
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();

        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                //here the condition to check login details

                }
            } catch (Exception e) {

            }
            return null;

        }

        protected void onPostExecute(Void result) {

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();

            }
        }
    }

and u can also use try,catch in catch block you can place your toast message with finsih() method

MGS
This should work. Just as clarification; the reason why you're getting an error is because in Android you can only do something UI-related on the UI thread, not on other threads. Using an AsyncTask takes care of this, since on{Pre,Post}Execute are executed on the UI thread. You could also use `Handler#post(Runnable)` from a secondary thread to get something done on the UI thread.
benvd