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.