【发布时间】:2016-10-06 17:07:40
【问题描述】:
我在使用setImageBitmap 时收到NullPointerException
“结果” 是有效位图。
这是我的代码:
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
try {
ImageView imageView1;
imageView1 = (ImageView) findViewById(R.id.imgbanner);
imageView1.setImageBitmap(result);
return;
} catch (Exception e) {
logMensajes("Error imageload onPostExecute: "+e.toString());
}
}
XML
<ImageView
android:id="@+id/imgbanner"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/txtTitulo"/>
【问题讨论】:
-
告诉我们你是如何得到
result的。 -
是否以活动形式 onPostExecute ?
-
提示:您可能不应该在 AsyncTask 中使用
findViewById。其次,请显示所有必要代码的minimal reproducible example 以获取此onPostExecute -
尝试在异步响应中添加
runOnUiThread(new Runnable(){ //your code }); -
应该在 AsyncTask 中调用 findViewById 吗?