Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:42:18+00:00 2026-06-14T09:42:18+00:00

When my android application throw an exception, I want to show a custom dialog

  • 0

When my android application throw an exception, I want to show a custom dialog to tell user there is something wrong happened, so I use Thread.setDefaultUncaughtExceptionHandler to set a global exception handler:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, final Throwable ex) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder.setTitle("There is something wrong")
                        .setMessage("Application will exit:" + ex.toString())
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // throw it again
                                throw (RuntimeException) ex;
                            }
                        })
                        .show();
            }
        });
    }

}

But I found it there is any exception thrown, the AlertDialog won’t show, instead, the application blocks and after a while, it will show a system dialog:

X app is not responding. Would you like to close it?
Wait  |  OK

What should I do now?


UPDATE

The log:

11-16 12:54:16.017: WARN/WindowManager(90): Attempted to add window with non-application token WindowToken{b38bb6a8 token=null}.  Aborting.

It seems the error is coming from new AlertDialog.Builder(getApplicationContext());

But this is an exception handler in Application subclass, how can I set an activity instance to it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:42:20+00:00Added an answer on June 14, 2026 at 9:42 am

    You cannot do any UI operation from here. Just start another activity/ splash screen. Pass an intent extra to denote crash and show dialog in that activity.

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.
         * lang.Thread, java.lang.Throwable)
         */
        @Override
        public void uncaughtException(Thread t, final Throwable e) {
            StackTraceElement[] arr = e.getStackTrace();
            final StringBuffer report = new StringBuffer(e.toString());
            final String lineSeperator = "-------------------------------\n\n";
            report.append(DOUBLE_LINE_SEP);
            report.append("--------- Stack trace ---------\n\n");
            for (int i = 0; i < arr.length; i++) {
                report.append( "    ");
                report.append(arr[i].toString());
                report.append(SINGLE_LINE_SEP);
            }
            report.append(lineSeperator);
            // If the exception was thrown in a background thread inside
            // AsyncTask, then the actual exception can be found with getCause
            report.append("--------- Cause ---------\n\n");
            Throwable cause = e.getCause();
            if (cause != null) {
                report.append(cause.toString());
                report.append(DOUBLE_LINE_SEP);
                arr = cause.getStackTrace();
                for (int i = 0; i < arr.length; i++) {
                    report.append("    ");
                    report.append(arr[i].toString());
                    report.append(SINGLE_LINE_SEP);
                }
            }
            // Getting the Device brand,model and sdk verion details.
            report.append(lineSeperator);
            report.append("--------- Device ---------\n\n");
            report.append("Brand: ");
            report.append(Build.BRAND);
            report.append(SINGLE_LINE_SEP);
            report.append("Device: ");
            report.append(Build.DEVICE);
            report.append(SINGLE_LINE_SEP);
            report.append("Model: ");
            report.append(Build.MODEL);
            report.append(SINGLE_LINE_SEP);
            report.append("Id: ");
            report.append(Build.ID);
            report.append(SINGLE_LINE_SEP);
            report.append("Product: ");
            report.append(Build.PRODUCT);
            report.append(SINGLE_LINE_SEP);
            report.append(lineSeperator);
            report.append("--------- Firmware ---------\n\n");
            report.append("SDK: ");
            report.append(Build.VERSION.SDK);
            report.append(SINGLE_LINE_SEP);
            report.append("Release: ");
            report.append(Build.VERSION.RELEASE);
            report.append(SINGLE_LINE_SEP);
            report.append("Incremental: ");
            report.append(Build.VERSION.INCREMENTAL);
            report.append(SINGLE_LINE_SEP);
            report.append(lineSeperator);
    
            Log.e("Report ::", report.toString());
            Intent crashedIntent = new Intent(BaseActivity.this, SplashActivity.class);
            crashedIntent.putExtra(EXTRA_CRASHED_FLAG,  "Unexpected Error occurred.");
            crashedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            crashedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(crashedIntent);
    
            System.exit(0);
            // If you don't kill the VM here the app goes into limbo
    
        }
    

    Also see:

    Android UncaughtExceptionHandler that instantiates an AlertDialog breaks

    Toast not showing up in UnCaughtExceptionHandler

    How to start activity from UncaughtExceptionHandler if this is main thread crashed?

    How i do it:

    I have a BaseActivity which extends Activity, and in onCreate of the activity I set the UncaughtExceptionHandler. All my activities extend the BaseActivity instead of Activity.

    Keys

    1. You can’t set the exception handler in Application.onCreate, instead, you should create a BaseActivity and set it on the onCreate method of it.
    2. After starting the SplashActivity, we should call System.exit(0)
    3. We can’t hold the error instance to share it to SplashActivity, since it will be destroyed, instead, we can pass some error message or persist it in file.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use slf4j with logback in my Android application (IDE Eclipse). I
I'm developing an Android 2.2 application. I want to catch and re throw the
I'm trying to use a custom class (be.myname.rssreader.util.Global) extending android.app.Application, but when I try
Android application should show Events happening in city as per day/month/year anywhere in the
Our android application is getting fairly big and we would like to use functional
I am developing android application which requires user to Login. This application relies on
I am trying to develop a custom stop watch application in android but it
I am making an Android application and I want to encrypt a String before
I have a class which extends android.app.Application , which I use to persist global
I have an Android application, and i want to store some generic view attributes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.