0

我有一个必须创建数据库的应用程序,如果失败了,那么前进就没有意义了。我已经建立了一个 AlertDialog 和show()它,但它从不显示。由于缺少数据库,逻辑失败然后失败。

抛出消息并停止活动的正确/最佳方式是什么?下面的代码执行得很好(意味着show()在调试时发生并且它落到下一行),但 UI 从未显示此警报。顺便说一句 - 我意识到投掷可能不是最优雅的,但我什至没有走那么远...... B^)。

try {

    myDBHelp.createDataBase();
} catch (IOException ioe) {
    new AlertDialog.Builder(this).setCancelable(false)
        .setMessage(ioe.getMessage())
        .setTitle("Database Create Failed")
        .setPositiveButton("Quit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                throw new Error("Unable to create database - please try uninstall/reinstall");
            }
         })
         .show();
4

3 回答 3

0

我不知道你使用什么流程。但是有一个建议你可以这样做。

你可以像这样开始数据库操作..

Intent i = new Intent(this,Databaseoperation.class); 开始活动(一);................................ 这将使控件移动到数据库操作类,它执行各种操作,如 open.close、insert删除..等

你可以在内置类中扩展数据库助手

现在,当打开数据库或任何东西出现任何问题时,完成()意图并返回主要活动......

你可以这样..

谢谢拉克什

于 2010-09-02T05:07:48.053 回答
0

我通常会做这样的事情:

void myFunction() {

    try {
        somecode..
    } catch (IOException e){
        e.printStackTrace();
        doToast("Unknown Error");  //Display Toast to user
        return;           //Leave myFunction
    }

    somecode...  //If no error continue here

    return;
}

protected void doToast(final String str) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(myClass.this, str, Toast.LENGTH_SHORT).show();
        }
    });

}
于 2010-09-02T16:21:50.967 回答
0

createDataBase()是抛出错误还是自行处理?如果它自己处理异常,那么它永远不会到达您的外部块,因此您永远不会通过 catch 块。确保throws IOException在方法签名的末尾添加如下:

public void createDataBase() throws IOException {[...]}

另外,请确保没有任何 try / catch 块.createDataBase()

这样,无论何时IOException发生,它都会被委派给您的外部 catch 块,并且您的对话框将出现。

于 2017-02-02T09:26:44.017 回答