I want the dialog to open when "AsyncTask" is "catch". I tried to call Dialogue into a “catch”. But the program is crashing. How do I open a dialog when there is a catch?
My code:
public class test extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
Document doc = (Document) Jsoup.connect("(warn: here my web)").get();
test = doc.text();
} catch (Exception e)
//Here problem dialog open. App crash
OpenDialog();
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
Toast.makeText(MainActivity.this, test, Toast.LENGTH_LONG).show();
}
}
logcat error (red):
2021-12-17 07:22:10.953 17530-17564/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process, PID: 17530
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:415)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: java.lang.RuntimeException: Can't create handler inside thread Thread[AsyncTask #1,5,main] that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:227)
at android.os.Handler.<init>(Handler.java:129)
at android.view.ViewRootImpl$ViewRootHandler.<init>(ViewRootImpl.java:5041)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:5369)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:763)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:399)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:111)
at android.app.Dialog.show(Dialog.java:342)
at Main.qaytaDialog(Main.java:130)
at Main.access$000(Main.java:24)
at Main$dooit.doInBackground(Main.java:95)
at Main$dooit.doInBackground(Main.java:83)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
CodePudding user response:
A bit broad but here are some tips. Displaying dialogs is foreground thing (runs on main or UI thread) so it can not be done in background method of async task (it runs on background Thread).It is only allowed from methods such as onPostExecute(), onPublishProgress().
Caused by: java.lang.RuntimeException: Can't create handler inside thread Thread[AsyncTask #1,5,main] that has not called Looper.prepare()
Please refer to this link for details
Here is a quick code snippet for your reference
public class MyAsync extends AsyncTask<Void, Void, Void> {
Boolean isCatched=false;
@Override
protected Void doInBackground(Void... voids) {
try {
if (!isCatched)
throw new Exception();
Log.i("Test", "doInBackground: ");
} catch (Exception e){
//Here problem dialog open. App crash
isCatched=true;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
if (isCatched)
OpenDialog();
Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_LONG).show();
}
}