Amrutha
Amrutha

Reputation: 575

Can't create handler inside thread that has not called Looper.prepare() in android

I am getting can't create handler inside thread in asynchronous background task. Below is my code. I made the necessary modifications to progress bar after searching in google but still the error is rising. Please help me with this. Will be thankful.

My code:

private class LongOperation1 extends AsyncTask<String, Void, String> {

        private final WeakReference<MainActivity> mainActivityWeakRef;
        ProgressDialog dialog;

        public LongOperation1(MainActivity mainActivity) {
            super();
            this.mainActivityWeakRef = new WeakReference<MainActivity>(
                    mainActivity);
            // this.activity = mainActivity;
        }

        @Override
        protected String doInBackground(String... params) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://.............php");

            // This is the data to send
            // String MyName = 'adil'; //any data to send
            // publishProgress(5);

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        1);
                nameValuePairs.add(new BasicNameValuePair("param1", "1"));
                nameValuePairs.add(new BasicNameValuePair("param2",
                        "Cities"));


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request

                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response = httpclient.execute(httppost, responseHandler);

                // This is the response from a php application
                String reverseString = response;
                Toast.makeText(MainActivity.this, "response" + reverseString,
                        Toast.LENGTH_LONG).show();

            } catch (ClientProtocolException e) {
                Toast.makeText(MainActivity.this,
                        "CPE response " + e.toString(), Toast.LENGTH_LONG)
                        .show();
                // TODO Auto-generated catch block
            } catch (IOException e) {
                Toast.makeText(MainActivity.this,
                        "IOE response " + e.toString(), Toast.LENGTH_LONG)
                        .show();
                // TODO Auto-generated catch block
            }
            return "All Done!";

        }

        @Override
        protected void onPostExecute(String result) {
            Log.d("onpostexecute", (mainActivityWeakRef.get() != null) + "");
            if (mainActivityWeakRef.get() != null
                    && !mainActivityWeakRef.get().isFinishing()) {

                AlertDialog alertDialog = new AlertDialog.Builder(
                        mainActivityWeakRef.get()).create();
                alertDialog.setTitle(result);
                alertDialog.setMessage("On post execute");
                alertDialog.setCancelable(false);
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });

                alertDialog.show();
            }

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            dialog.incrementProgressBy(5);
        }
    }

Upvotes: 0

Views: 404

Answers (2)

Chaitu
Chaitu

Reputation: 917

You can not update the UI thread(Main Thread) from another thread.. If you want to do it

1) Return your response String from doInbackground, and update the UI in PostExecute()

2)Otherwise you can wrap the Toast Message in runonUiThread(){}

3) Use Handler to update the UI from another thread.

Upvotes: 1

AlexBalo
AlexBalo

Reputation: 1268

You cannot use Toast in the do in background operation because you need the UIThread to show them. Instean of doing that as you did. Use a variable an save it for different states.

In onPostExecute check the variable and show the corresponding Toast.

Hope it helps.

Upvotes: 0

Related Questions