1

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) 

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
NONAME
  • 37
  • 7
  • Are you getting any error messages? If you are, can you please [edit] them into your post? Thanks :) – Ekadh Singh - Reinstate Monica Dec 17 '21 at 15:04
  • I can't understand the problem. If I don't add a dialog call‚ it works without error. However, if I add a call dialog output, the program crashes. – NONAME Dec 17 '21 at 15:07
  • Do you get any errors when the program crashes? – Ekadh Singh - Reinstate Monica Dec 17 '21 at 15:12
  • openDialog() code or any stack trace details would be helpful – Aashis Shrestha Dec 17 '21 at 15:30
  • 1
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) –  Dec 17 '21 at 16:44

1 Answers1

1

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();
}

}

Aashis Shrestha
  • 342
  • 5
  • 13
  • check out my new question‚ please. https://stackoverflow.com/questions/70972347/determine-if-the-problem-is-on-the-server-or-the-internet-jsoup-asynctask – NONAME Feb 03 '22 at 15:06