Android 从Url设置RelativeLayout的背景
我想从Android 从Url设置RelativeLayout的背景,android,bitmap,android-drawable,android-relativelayout,Android,Bitmap,Android Drawable,Android Relativelayout,我想从URL获取图像,并将其设置为我的RelativeLayout的背景。我使用AsyncTask并尝试从bitmappdrawable创建一个Drawable。但是我得到了一个NullPointerException,似乎不知道为什么 class DownloadBackgroundTask extends AsyncTask<String, Void, Bitmap> { RelativeLayout bmImage; Vie
URL
获取图像,并将其设置为我的RelativeLayout
的背景。我使用AsyncTask
并尝试从bitmappdrawable
创建一个Drawable
。但是我得到了一个NullPointerException
,似乎不知道为什么
class DownloadBackgroundTask extends AsyncTask<String, Void, Bitmap> {
RelativeLayout bmImage;
View vi;
public DownloadBackgroundTask(RelativeLayout bmImage, View vi) {
this.bmImage = bmImage;
this.vi = vi;
}
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) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if (result != null)
{
// null pointer exception here
Drawable d = new BitmapDrawable(vi.getContext().getResources(), result);
bmImage.setBackground(d);
}
else
{
bmImage.setBackgroundResource(R.drawable.logo);
}
}
}
getContext()是否返回null?或getResources()返回null?。尝试拆分它们,看看哪个返回null.No。Nvm我的相对布局中没有id标签
RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.RelativeLayout1);
new DownloadBackgroundTask(rl, rootView).execute("http://pctechmag.com/wp-content/uploads/2012/10/fifa13_messi.jpg");