让我抓狂的是,我的程序在 try 块中间停止,在所有 catch 块之后继续!以下是详细信息。我有异步任务
public class BigBitmapLoader extends AsyncTask<Uri, Void, Bitmap>
{
public BigBitmapLoader(ScribblesView scribbles)
{
scribblesRef = new WeakReference<ScribblesView>(scribbles);
}
@Override
protected Bitmap doInBackground(Uri... params)
{
InputStream is;
try
{
ScribblesView scribs = scribblesRef.get();
if (scribs != null)
{
is = scribs.getContext().getContentResolver().openInputStream(params[0]);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return bitmap;
}
}
catch(FileNotFoundException e)
{
Log.e(ERROR_TAG, e.toString());
}
catch(IOException e)
{
Log.e(ERROR_TAG, e.toString());
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
ScribblesView scribs = scribblesRef.get();
if (scribs != null) scribs.setBigBitmap(bitmap);
}
private WeakReference<ScribblesView> scribblesRef;
private static final String ERROR_TAG = "BigBitmapLoader";
}
在 doInBackground() 中,它到达is.close()
然后立即跳转到return null
所有 catch 块之后。因此它跳过return bitmap
。在这一点上,我没有例外。只有稍后使用返回的位图时,我才得到 NPE。有任何想法吗?