我正在尝试通过 url 将背景图像添加到 ImageButton。
我已经从 Google Android 文档中修改了 downloadimagetask,如下所示。
class DownloadImageButtonTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageButtonTask(ImageButton bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
我收到错误::Caused by: java.lang.NullPointerException: println 需要一条消息
在这一行::Log.e("Error", e.getMessage());
我将此异步任务称为
new DownloadImageButtonTask((ImageButton) findViewById(R.id.my_image_btn))
.execute("any image url");
最佳答案
设置 尝试设置
if( e.getMessage()!=null)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
或者简单地
String exception= (e.getMessage()!=null)?ex.getMessage():"Exception occured";
Log.e("Error", exception);
关于java - Android通过url设置ImageButton背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16198609/