0

I"m trying to read contacts from phone on button click

but i'm getting the error "Can't create handler inside thread that has not called Looper.prepare()"

pDialog = new ProgressDialog(this);
        pDialog.setMessage("Reading contacts...");
        pDialog.setCancelable(false);

        updateBarHandler = new Handler();
        //putting read contacts on a separate thread

        mPickContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                       pDialog.show();
                        getContacts();
                    }

                }).start();

            }
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
grace
  • 63
  • 2
  • 10
  • use asynctask. show dialog in `onPreExecute `and dismiss the same in `onPostExecute`. `getContacts()` in `doInbackground` you need to remove `pDialog.show()` from your thread – Raghunandan Oct 16 '17 at 09:31
  • Hi, i'm new to Android development. Can you please share a working code example – grace Oct 16 '17 at 09:33
  • check the link marked at the top of the post – Raghunandan Oct 16 '17 at 09:34
  • The solution for your ProgressDialog to work correctly is to execute the time consuming code in a separate thread while the Dialog is shown in the main tthread. It seems that you try to do just the reversed. The code that manages the UI, dialogs included, must always be executed in the main thread, the UI thread. – from56 Oct 16 '17 at 09:39

0 Answers0