首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何捕获异常并使用错误自定义对话框进行修改

如何捕获异常并使用错误自定义对话框进行修改
EN

Stack Overflow用户
提问于 2012-10-11 06:49:41
回答 2查看 416关注 0票数 0

我在我的应用程序中已经有了这个异常,但是我想在这里创建一个自定义对话框,这是我的异常:

代码语言:javascript
运行
复制
10-11 01:41:49.420: E/AACPlayer(649): playAsync():
10-11 01:41:49.420: E/AACPlayer(649): java.lang.RuntimeException: 
Cannot start native decoder

而不是显示此java.lang.RuntimeException:无法启动本机解码器的人,显示像错误!!溪流下降了。

我试过这样的方法:

代码语言:javascript
运行
复制
 try
        {
            aacPlayer.playAsync( getUrl());
        }
        catch(Exception e)
        {
            AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
            alertDialog.setTitle("Error in radio");
            alertDialog.setMessage("out of service");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                  // here you can add functions
               }
            });
            alertDialog.setIcon(R.drawable.icon);
            alertDialog.show();
        }

但如果没有运气,它没有显示自定义对话框,什么可以推荐我这样做?

非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-11 07:04:30

看到它,也许它能帮助你;)

如何捕捉“java.lang.RuntimeException”抛出和捕获异常

票数 1
EN

Stack Overflow用户

发布于 2012-10-11 07:10:22

如果对话框中有一个已经弹出的自定义消息,它会工作吗?如果是,那么您可能希望在catch块中这样做:

代码语言:javascript
运行
复制
CustomException ce = new CustomException();
throw ce;

并且有这样的自定义异常

代码语言:javascript
运行
复制
public static class CustomException extends Exception {

    String errorMsg; // this is the string that the your alert box will show

    public CustomException() {
        super();
        errorMsg = "YOU DID THIS ALL"; // set string here for your custom message
    }

    public CustomException(String err) { // set custom string in the constructor itself
        super(err);
        errorMsg = err;
    }

    public String getError() {
        return errorMsg;
    }

    public String toString() {
        return errorMsg;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12833761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档