1

Here is my code: ("Apagar" means "Turn off", "reiniciar" means "restart")

public void uncaughtException(Thread thread, Throwable ex)
{
    try
    {
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(iAct).create();
        //alertDialog.setTitle("");
        alertDialog.setMessage("Se ah producído un error y la aplicación no puede continuar funcionando." +
                               "¿Desea reiniciarla o finalizarla?");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Reiniciar", new ReiniciarOnClickListener(iAct));
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Apagar", new ApagarOnClickListener(iAct));
        alertDialog.show();
    }catch(Exception e){
        Thread.setDefaultUncaughtExceptionHandler(iDefaultHandler);
        iDefaultHandler.uncaughtException(thread, ex);
    }
}

All the code inside Try block is executed, but the alert is never shown, and the screen stays grey.

In iDefaultHandler I saved the result of getDefaultUncoughtException() before seting mine.

Any information that is missing, please ask for it. I don't know how to solve this.

4

2 回答 2

0

您是否尝试过使用onCreateDialog来构建对话框?就像是:

private static final int DIALOG_UNCAUGHT_EXCEPTION = 0;

...

public void uncaughtException(Thread thread, Throwable ex)
{
    try
    {
        showDialog(DIALOG_UNCAUGHT_EXCEPTION);
    }catch(Exception e)
    {
        Thread.setDefaultUncaughtExceptionHandler(iDefaultHandler);
        iDefaultHandler.uncaughtException(thread, ex);
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch (id) {
    case DIALOG_UNCAUGHT_EXCEPTION:
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setMessage("message")
            .setPositiveButton("Reiniciar",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do stuff;
                }
            })
            .setNegativeButton("Apagar",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do other stuff;
                }
            });
        alertDialog.create();
        break;
    default:
        dialog = null;
        break;
    }
    return dialog;
}

显然,如果需要,您可以用自己的替换默认按钮侦听器。

于 2011-01-06T12:19:06.960 回答
0

尝试取出 .create() 。只需初始化 AlertDialog 然后显示它。

于 2011-01-05T21:15:25.123 回答