Nullpointerexception in displaying image from json url Android -
so here's code i'm working on:
public class mainactivity extends listactivity { // url make request private static string url = "http://alyssayango.x10.mx/"; private static final string tag_type = "movie_type"; private static final string tag_name = "movie_name"; private static final string tag_length = "movie_length"; private static final string tag_schedules = "movie_schedules"; private static final string tag_cinema = "movie_cinema_number"; private static final string tag_url = "movie_image_url"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mytask task = new mytask(mainactivity.this); task.execute(url); } public string readmovieschedules(string url) { stringbuilder builder = new stringbuilder(); httpclient client = new defaulthttpclient(); httpget httpget = new httpget(url); try { httpresponse response = client.execute(httpget); statusline statusline = response.getstatusline(); int statuscode = statusline.getstatuscode(); if (statuscode == 200) { httpentity entity = response.getentity(); inputstream content = entity.getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(content)); string line; while ((line = reader.readline()) != null) { builder.append(line); } } else { log.e(mainactivity.class.tostring(), "failed download file"); } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return builder.tostring(); } private class mytask extends asynctask<string, void, string>{ private progressdialog pd; public mainactivity activity; public mytask(mainactivity a) { activity = a; } @override protected string doinbackground(string... urls) { string stringtoparse=null; (string url : urls) { stringtoparse = readmovieschedules(url); // getting xml url } return stringtoparse; } @override protected void onpreexecute() { // todo auto-generated method stub pd = new progressdialog(mainactivity.this); pd.settitle("downloading..."); pd.setmessage("please wait."); pd.setcancelable(false); pd.setindeterminate(true); pd.show(); super.onpreexecute(); } @override protected void onpostexecute(string readmovieschedules) { // hashmap listview arraylist<hashmap<string, string>> movielist = new arraylist<hashmap<string, string>>(); jsonarray jsonarray; try { jsonarray = new jsonarray(readmovieschedules); log.i(mainactivity.class.getname(), "number of entries " + jsonarray.length()); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); log.i(mainactivity.class.getname(), jsonobject.getstring("movie_name")); // storing each json item in variable string name = jsonobject.getstring(tag_name); string type = jsonobject.getstring(tag_type); string length = jsonobject.getstring(tag_length); string cinema = jsonobject.getstring(tag_cinema); string schedules = jsonobject.getstring(tag_schedules); string url = jsonobject.getstring(tag_url); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_name, name); map.put(tag_type, type); map.put(tag_length, length); map.put(tag_cinema, cinema); map.put(tag_schedules, schedules); map.put(tag_url, url); // adding hashlist arraylist movielist.add(map); /** * updating parsed json data listview * */ line 178 listadapter adapter = new customadapter(mainactivity.this, movielist, r.layout.list_item, new string[] { tag_name, tag_cinema, tag_schedules, tag_url }, new int[] { r.id.name, r.id.cinema, r.id.schedules, r.id.image }); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); // launching new screen on selecting single listitem lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); string cost = ((textview) view.findviewbyid(r.id.cinema)).gettext().tostring(); string description = ((textview) view.findviewbyid(r.id.schedules)).gettext().tostring(); string url = ((textview) view.findviewbyid(r.id.image_)).gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(tag_name, name); in.putextra(tag_cinema, cost); in.putextra(tag_schedules, description); in.putextra(tag_url, url); startactivity(in); } }); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } pd.dismiss(); super.onpostexecute(readmovieschedules); } } private class customadapter extends simpleadapter { public imageloader imageloader; public layoutinflater inflater = null; private activity activity; private context mcontext; private url url; private bitmap bmp; public customadapter(context context, list<? extends map<string, ?>> data, int resource, string[] from, int[] to) { super(context, data, resource, from, to); mcontext = context; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); line 238 imageloader = new imageloader(activity.getapplicationcontext()); } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = getlayoutinflater().inflate(r.layout.list_item, null); } hashmap<string, object> data = (hashmap<string, object>) getitem(position); textview name = (textview) convertview.findviewbyid(r.id.name); textview cinema = (textview) convertview.findviewbyid(r.id.cinema); textview schedules = (textview) convertview.findviewbyid(r.id.schedules); imageview image = (imageview) convertview.findviewbyid(r.id.image); string namestring = (string) data.get(tag_name); string cinemastring = (string) data.get(tag_cinema); string schedulesstring = (string) data.get(tag_schedules); string imageurl = (string) data.get(tag_url); name.settext(namestring); cinema.settext(cinemastring); schedules.settext(schedulesstring); try { url newurl = new url(imageurl); bmp = bitmapfactory.decodestream(newurl.openconnection().getinputstream()); image.setimagebitmap(bmp); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } imageloader.displayimage(imageurl, (activity)mcontext, image); return convertview; } } }
i'm having error/nullpointerexception in these lines:
listadapter adapter = new customadapter(mainactivity.this, movielist, r.layout.list_item, new string[] { tag_name, tag_cinema, tag_schedules, tag_url }, new int[] { r.id.name, r.id.cinema, r.id.schedules, r.id.image });
what thing i'm missing in here? appreciated. thanks.
updated log file:
07-25 10:39:58.564: e/androidruntime(971): fatal exception: main 07-25 10:39:58.564: e/androidruntime(971): java.lang.nullpointerexception 07-25 10:39:58.564: e/androidruntime(971): @ com.say.stalucia.mainactivity$customadapter.<init>(mainactivity.java:238) 07-25 10:39:58.564: e/androidruntime(971): @ com.say.stalucia.mainactivity$mytask.onpostexecute(mainactivity.java:178) 07-25 10:39:58.564: e/androidruntime(971): @ com.say.stalucia.mainactivity$mytask.onpostexecute(mainactivity.java:1) 07-25 10:39:58.564: e/androidruntime(971): @ android.os.asynctask.finish(asynctask.java:631) 07-25 10:39:58.564: e/androidruntime(971): @ android.os.asynctask.access$600(asynctask.java:177) 07-25 10:39:58.564: e/androidruntime(971): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644)
line 238:
imageloader = new imageloader(activity.getapplicationcontext());
line 178:
listadapter adapter = new customadapter(mainactivity.this, movielist,
activity
null in line:
imageloader = new imageloader(activity.getapplicationcontext());
as have not assigned in constructor.
why not use context object instead?
imageloader = new imageloader(mcontext);
Comments
Post a Comment