在catch块中使用Toast时不显示

时间:2010-10-31 19:27:47

标签: android exception show toast catch-block

我注意到在catch块中使用toast时不显示toast。 有没有人知道如何在捕获异常时显示祝酒词?一个例子:

try {
    // try to open a file
} catch (FileNotFoundException e) {
    Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
    return; // cancel processing
}

2 个答案:

答案 0 :(得分:14)

应该是这样的:

Toast toast = Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
toast.show();

答案 1 :(得分:11)

是的,我把它放在现有的一行之后:

Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG).show();