Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:42:48+00:00 2026-06-08T23:42:48+00:00

I am doing mutlithreading in my program with AsyncTask by calling a Method after

  • 0

I am doing mutlithreading in my program with AsyncTask by calling a Method after 10 secs but instead having this Exception :

08-04 11:49:29.565: E/log_tag(885): Error converting result java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Plz help me !!

Edit: This is code for AsyncTask:

  class Getphonenumber extends AsyncTask<String, Void, Void> {
         public Void doInBackground(String... p) {
          while (true) {
              getPhno();
           try {
            Thread.sleep(10000);
           } catch (InterruptedException ie) {
            ie.printStackTrace();
           }
          }
        }

        };

‘getPhno()’ method:

public void getPhno()
  {
      try{

            HttpClient httpclient = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit
            List<NameValuePair> username = new ArrayList<NameValuePair>();
            username.add(new BasicNameValuePair("username", user));
            //Web Address
            HttpPost httppost = new HttpPost("http://www.starretailshop.com/Log/Retrieve.php");
            httppost.setEntity(new UrlEncodedFormEntity(username));
            HttpResponse response = httpclient.execute(httppost);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost,
                    responseHandler);

            Log.i("retrieve postData", response.getStatusLine().toString());
            InputStream content = response.getEntity().getContent();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {

                sendMsg(s,msg);

            }

            }catch(Exception e){

                  Log.e("log_tag", "Error converting result "+e.toString());
            }
  }

‘sendMsg’ Method:

private void sendMsg(String phoneNumber, String message)
    { 
       try{
            String phoneNumber1 = null;

           if(phoneNumber.startsWith("+"))
            {
                phoneNumber1 = phoneNumber.substring(3);

            }
            else if(phoneNumber.startsWith("0"))
            {
                phoneNumber1 = phoneNumber.substring(1);
            }
            else
            {
                phoneNumber1=phoneNumber;
            }
                PendingIntent pi = PendingIntent.getActivity(this, 0,
                    new Intent(), 0);                
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber1, null, message, pi, null);
                Toast.makeText(this,"Msg sent to:" + phoneNumber1, Toast.LENGTH_LONG).show();
                phnoList.add(phoneNumber);
                phnoListd.add(phoneNumber1);
                //phnumber = phoneNumber;
                //================Update Database=====================

              try{
                  HttpClient httpclient = new DefaultHttpClient();
                  HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit
                  List<NameValuePair> updatemsg = new ArrayList<NameValuePair>();
                  updatemsg.add(new BasicNameValuePair("outmsg1", message));
                  Calendar cal = Calendar.getInstance();
                  SimpleDateFormat sdf = new SimpleDateFormat("HH:m:ss dd-MM-yyyy");
                  String TimeStampDB = sdf.format(cal.getTime()); 
                  updatemsg.add(new BasicNameValuePair("currentdatetime1",TimeStampDB));
                  updatemsg.add(new BasicNameValuePair("mobileno",phoneNumber));

                    //Web Address
                    HttpPost httppost = new HttpPost("http://www.starretailshop.com/Log/Update1.php");
                    httppost.setEntity(new UrlEncodedFormEntity(updatemsg));
                    HttpResponse response = httpclient.execute(httppost);
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    String responseBody = httpclient.execute(httppost,
                            responseHandler);
                    Log.i("update1 postData", response.getStatusLine().toString());
                    }catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                    }

                    //================================================
          }catch(Exception e)
          {
              e.printStackTrace();
              Toast.makeText(this,"Error occured", Toast.LENGTH_LONG).show();
          }


}

Finally I am calling new Getphonenumber().execute(); in an onClickListener of a Button.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:42:52+00:00Added an answer on June 8, 2026 at 11:42 pm

    What I think to be the problem is, you are trying to update your UI components from your background Thread.

    For example, consider this method in your AsyncTask,

    protected Void doInBackground(String ... x) {
          while (true) {
           System.out.println(x[0]);
           try {
            Thread.sleep(1000);
           } catch (InterruptedException ie) {
            ie.printStackTrace();
           }
          }
    

    This method is just equivalent to a background thread. And in Andorid you can update your UI only form the main UI thread and when you try to do that from any other method you will get this exception.

    So try to avoid this. If you want to update your UI, use the onPostExecute() of your AsyncTask.

    EDIT 1

    here is your problem. Remove the toast from sendMsg method and it should work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'
Doing a simple Squeryl database lookup, but trying to exclude a value. I've tried:
I am doing a port scanner program as part of a school project where
I'm writing a multithreaded program, which would crash when a particular exception was thrown.
I am new to Java Multithreading World, I wrote this program, I just wanted
Doing Android development on a Mac and this very new phone I have doesn't
Doing a Socket.Receive(byte[]) will get the bytes in from the buffer, but if the
I have a little problem, I wrote a program, server role, doing an infinite
I have created this little program to calculate pi using probability and ratios. In
Doing an ajax get request works as expected using the following code: $.ajax({ type:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.