通過URL獲取圖片時,然後在imageView中設置,結果系統給我報出空指針異常。
請高手幫忙解決一下,謝謝。
public class DetailsActivity extends Activity {
ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
final Bundle extras = getIntent().getExtras();
String img = extras.getString("Thumbnail");
new DownloadImageTask((ImageView) findViewById(R.id.btnThumbnail))
.execute("http://mysite.com/images/"
+ img);
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
public DownloadImageTask(ImageView bmImage) {
thumbnail = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Log.e("URL",urldisplay);
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) {
thumbnail.setImageBitmap(result);
}
}
}
寫法沒問題,只不過thumbnail不應該在外部去查找指向他的id
ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);
改成:
ImageView thumbnail即可