android - i am getting null pointer exception when i am trying to download image... -
i want download image internet ...but getting null pointer exception want know major difference bw url connection , http connection if there
pls tell tell me solution
here code thank u
public class detailsactivity extends activity { private textview textview1; private textview textview2; private textview textview3; private textview textview4; private textview textview5; //private imageview image; imageview image; static bitmap bm; progressdialog pd; string imageurl = "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png"; bitmapfactory.options bmoptions; public class imagedownload extends asynctask<string, void, string> { protected string doinbackground(string... params) { // todo auto-generated method stub bmoptions = new bitmapfactory.options(); bmoptions.insamplesize = 1; loadbitmap(imageurl, bmoptions); return imageurl; } protected void onpostexecute(string imageurl) { pd.dismiss(); if (!imageurl.equals("")) { image.setimagebitmap(bm);//error here null pointer exception } else { toast.maketext(detailsactivity.this, "não foi possível obter resultados", toast.length_long) .show(); } } } public static bitmap loadbitmap(string url, bitmapfactory.options options) { inputstream in = null; try { in = openhttpconnection(url); bm = bitmapfactory.decodestream(in, null, options); in.close(); } catch (ioexception e1) { } return bm; } private static inputstream openhttpconnection(string strurl) throws ioexception { inputstream inputstream = null; url url = new url(strurl); urlconnection conn = url.openconnection(); try { httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setrequestmethod("get"); httpconn.connect(); if (httpconn.getresponsecode() == httpurlconnection.http_ok) { inputstream = httpconn.getinputstream(); } } catch (exception ex) { } return inputstream; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); image = (imageview)findviewbyid(r.id.imageview2); actionbar actionbar = getactionbar(); actionbar.hide(); setcontentview(r.layout.activity_details); pd = progressdialog.show(detailsactivity.this, "testing", "loading"); new imagedownload().execute(""); //asyncdownload(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.details, menu); textview1 = (textview)findviewbyid(r.id.textdetailplace); textview2 = (textview)findviewbyid(r.id.textdetailaddress ); textview3 = (textview)findviewbyid(r.id.textcapacity); // textview4 = (textview)findviewbyid(r.id.textdetailcontactno); textview5 = (textview) findviewbyid(r.id.textviewdescription); textview1.settext(getintent().getextras().getstring("test")); textview2.settext(getintent().getextras().getstring("test2")); textview3.settext(getintent().getextras().getstring("test3")); //textview4.settext(getintent().getextras().getstring("test4")); textview5.settext(getintent().getextras().getstring("test5")); return true; }
}
put image = (imageview)findviewbyid(r.id.imageview2);
after set content view.
you can find view after setting view activity.then identifies id form xml layout.
setcontentview(r.layout.activity_details); image = (imageview)findviewbyid(r.id.imageview2);
Comments
Post a Comment