0

我正在尝试通过 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");
4

1 回答 1

1

设置尝试设置

if( e.getMessage()!=null)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}

或者干脆

String exception= (e.getMessage()!=null)?ex.getMessage():"Exception occured";
 Log.e("Error", exception);
于 2013-04-24T17:38:39.483 回答