【发布时间】:2013-10-21 09:50:40
【问题描述】:
我正在使用一种方法获取网站的图标。当然,并不是每个网站都有网站图标。所以我想抓住它。如果网站没有网站图标,应用程序不会崩溃,但我仍然在 LogCat 中收到 FileNotFoundException。
我遇到的问题是抓不到
当我添加 `catch (FileNotFoundException f)
它告诉我的 try-catch 块
Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body.
我的选择是删除它或向 doInBackground 方法添加 throws 声明。后者是不可能的。这就是整个 Try-Catch
try{
String baseURL = getBaseURL ( sourceURLArr[i] );
System.out.println(baseURL + "/favicon.ico");
Bitmap favicon = getBitmapFromURL( baseURL + "/favicon.ico");
Drawable fv = new BitmapDrawable(getResources(),
Bitmap.createScaledBitmap(favicon, 20, 20, true));
source [i].setCompoundDrawablesWithIntrinsicBounds(fv, null, null, null);
} catch(NullPointerException e){
} catch(FileNotFoundException f){
}
我已经尝试将 FileNotFoundException 与 NullPointerException 切换,但这是同样的错误。当我在后台方法中将 throws 添加到异步任务中时,我得到了
Exception FileNotFoundException is not compatible with throws clause in AsyncTask<Void,Void,Void>.doInBackground(Void[])
我现在如何捕获 FileNotFoundException?
【问题讨论】:
-
你没有处理任何文件..那你为什么要使用 FileNotFoundException ?
-
好吧,我的 LogCat 告诉我这是一个 FileNotFoundException。我正在下载 favicon.ico - 或者,我想下载它。如果它不可用,它是一个 FileNotFoundException,因为该文件不存在并且尚未找到,或者我是否理解某事。错了吗?
-
所以最好在 Bitmap favicon = getBitmapFromURL(baseURL + "/favicon.ico");
-
getBitmapFromURL 函数的代码是什么?
标签: java android exception-handling android-asynctask