-1

In my android application, dialog's showing up takes a time. At that time I want to show progress dialog. I learned that the progress dialog should be executed in thread but when I use a thread it gives an error.

I created progress dialog in oncreate method and tried to show in my button's onclick method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    mProgressDialog = new ProgressDialog(context, R.style.StyledDialog);
    mProgressDialog.setCanceledOnTouchOutside(false);
    Drawable drawable = context.getResources().getDrawable(R.drawable.progress_dialog);
    mProgressDialog.setProgressDrawable(drawable);
}

Following code is my button's onclick method which is defined in xml file like : android:onClick="refList"

public void refList(View v) {


    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {

            mProgressDialog.show();

        }
    });
    t.start();

    if(!refListDialog.isShowing()) {
        refListDialog.show();
        t.interrupt();

    }

}

This is the exception:

 FATAL EXCEPTION: Thread-27305 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:121)
        at android.view.ViewRootImpl$ViewRootHandler.<init>(ViewRootImpl.java:3052)
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:3321)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:294)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
        at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:151)
        at android.app.Dialog.show(Dialog.java:277)
        at gcm.b4deploy.com.hesapozeti.MainActivity$2.run(MainActivity.java:196)
        at java.lang.Thread.run(Thread.java:856)

I am really stuck and waiting ideas. Thanks in advance.

Daniel
  • 2,355
  • 9
  • 23
  • 30
Hilal
  • 902
  • 2
  • 22
  • 47

2 Answers2

0

Try like this

        runOnUiThread(new Runnable() {
        public void run() {
        mProgressDialog.show();
        }
    });
akhil Rao
  • 1,169
  • 7
  • 17
  • Still getting error: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.. – Hilal May 25 '16 at 07:03
0

I really don't get the idea why you put the progressDialog inside a thread.

yourProgress = new ProgressDialog(this);
yourProgress.setTitle("Title");
yourProgress.setMessage("wait for a while"); yourProgress.getProcess();
yourProgress.show(); 

Put this code first in your function in the onClick method. It will execute sequentially.And you can make conditional statement, and if done, call yourProgress.dismiss();

sample code - add your statement above the progressDialog.setTitle. I suggest you declare the progress dialog outside this method, so that you can dismiss that one in the other method which return true. Just remove those runnable thing.

RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56
  • When I did as your said it gives an error: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener... – Hilal May 25 '16 at 06:47
  • If (statement) { yourProgressDialog... Then your statement }else{} – RoCkDevstack May 25 '16 at 07:47
  • Thanks, the problem is that I used getApplicationContext() instead of MainActivity.this. No need to use thread. – Hilal May 25 '16 at 09:02
  • Do you remove the runnable? The thread you just created? getApplicationContext() is suitable for fragments. In activity user the CLASS_NAME.this . (Glad to help even though it lack more instruction) – RoCkDevstack May 25 '16 at 09:13
  • I have already remove it. But it was still getting an error. Then I create onclick method like yours (not defined in xml by defining .setOnClickListener()....) then the error is changed. Then I googled that error and used again stackoverflow (http://stackoverflow.com/questions/5796611/dialog-throwing-unable-to-add-window-token-null-is-not-for-an-application-wi) then it solved. Thanks again. – Hilal May 25 '16 at 10:28