Loading images through url in android -
iam loading images in listview url image decoding in background getting following errors.
package com.abc; import java.io.inputstream; import java.net.url; import java.sql.array; import java.util.arraylist; import java.util.hashmap; import java.util.list; import org.apache.http.namevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.r.integer; import android.app.listactivity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.drawable.drawable; import android.media.image; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; public class foodview extends listactivity { private progressdialog pdialog; private string[] mstrings; jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> productslist; private static string url_all_products = "http://ambrogroup.com/app/index/file/food"; private static final string tag_success = "success"; private static final string tag_products = "products"; private static final string tag_pid = "pid"; private static final string tag_name = "name"; private static final string tag_image = "img"; private static final string tag_destination = "destination"; private static final string tag_price = "price"; jsonarray products = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.food_view); // productslist = new arraylist<hashmap<string, string>>(); // hashmap listview new loadallproducts().execute(); // loading products in background thread explicit function listview listview = getlistview(); // listview listview.setonitemclicklistener(new onitemclicklistener() { // on seleting single product // launching edit product screen @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //getting values selected listitem string pid = ((textview) view.findviewbyid(r.id.pid)).gettext().tostring(); string pname = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); string pimg = ((textview) view.findviewbyid(r.id.img)).gettext().tostring(); string pdestination = ((textview) view.findviewbyid(r.id.destination)).gettext().tostring(); string pprice = ((textview) view.findviewbyid(r.id.price)).gettext().tostring(); } }); } class loadallproducts extends asynctask<string, string, string> { listview listview; arraylist<itemlist> listarray = new arraylist<itemlist>(); customlistviewadapter customgridadapter; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(foodview.this); pdialog.setmessage("loading list. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting products url * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_products, "get", params); // check log cat json reponse log.d("getting list : ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { products = json.getjsonarray(tag_products); (int = 0; < products.length(); i++) { jsonobject c = products.getjsonobject(i); string id = c.getstring(tag_pid); string name = c.getstring(tag_name); string img = c.getstring(tag_image); string destination = c.getstring(tag_destination); string price = c.getstring(tag_price); mstrings[i] = img; string urldisplay = mstrings[i]; 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(); } listarray.add(new itemlist(micon11,id,name,destination,price)); listview = (listview) findviewbyid(r.id.list_item); customgridadapter = new customlistviewadapter(foodview.this, r.layout.row_grid, listarray); listview.setadapter(customgridadapter); } } else { } } catch (jsonexception e) { e.printstacktrace(); } return null; } } }
getting following errors
04-23 05:15:15.839: e/androidruntime(1398): fatal exception: asynctask #1 04-23 05:15:15.839: e/androidruntime(1398): process: com.ambrogroup, pid: 1398 04-23 05:15:15.839: e/androidruntime(1398): java.lang.runtimeexception: error occured while executing doinbackground() 04-23 05:15:15.839: e/androidruntime(1398): @ android.os.asynctask$3.done(asynctask.java:300) 04-23 05:15:15.839: e/androidruntime(1398): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 04-23 05:15:15.839: e/androidruntime(1398): @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 04-23 05:15:15.839: e/androidruntime(1398): @ java.util.concurrent.futuretask.run(futuretask.java:242) 04-23 05:15:15.839: e/androidruntime(1398): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 04-23 05:15:15.839: e/androidruntime(1398): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 04-23 05:15:15.839: e/androidruntime(1398): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 04-23 05:15:15.839: e/androidruntime(1398): @ java.lang.thread.run(thread.java:841) 04-23 05:15:15.839: e/androidruntime(1398): caused by: java.lang.nullpointerexception 04-23 05:15:15.839: e/androidruntime(1398): @ com.ambrogroup.foodview$loadallproducts.doinbackground(foodview.java:129) 04-23 05:15:15.839: e/androidruntime(1398): @ com.ambrogroup.foodview$loadallproducts.doinbackground(foodview.java:1) 04-23 05:15:15.839: e/androidruntime(1398): @ android.os.asynctask$2.call(asynctask.java:288) 04-23 05:15:15.839: e/androidruntime(1398): @ java.util.concurrent.futuretask.run(futuretask.java:237) 04-23 05:15:15.839: e/androidruntime(1398): ... 4 more 04-23 05:15:15.899: i/choreographer(1398): skipped 43 frames! application may doing work on main thread. 04-23 05:15:16.339: i/choreographer(1398): skipped 196 frames! application may doing work on main thread. 04-23 05:15:16.659: i/choreographer(1398): skipped 206 frames! application may doing work on main thread. 04-23 05:15:17.009: i/choreographer(1398): skipped 222 frames! application may doing work on main thread. 04-23 05:15:17.819: i/choreographer(1398): skipped 527 frames! application may doing work on main thread. 04-23 05:15:18.139: i/choreographer(1398): skipped 124 frames! application may doing work on main thread. 04-23 05:15:18.769: e/windowmanager(1398): android.view.windowleaked: activity com.ambrogroup.foodview has leaked window com.android.internal.policy.impl.phonewindow$decorview{b50b0648 v.e..... r.....id 0,0-304,96} added here 04-23 05:15:18.769: e/windowmanager(1398): @ android.view.viewrootimpl.<init>(viewrootimpl.java:348) 04-23 05:15:18.769: e/windowmanager(1398): @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:248) 04-23 05:15:18.769: e/windowmanager(1398): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.dialog.show(dialog.java:286) 04-23 05:15:18.769: e/windowmanager(1398): @ com.ambrogroup.foodview$loadallproducts.onpreexecute(foodview.java:98) 04-23 05:15:18.769: e/windowmanager(1398): @ android.os.asynctask.executeonexecutor(asynctask.java:587) 04-23 05:15:18.769: e/windowmanager(1398): @ android.os.asynctask.execute(asynctask.java:535) 04-23 05:15:18.769: e/windowmanager(1398): @ com.ambrogroup.foodview.oncreate(foodview.java:63) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.activity.performcreate(activity.java:5231) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.activitythread.access$800(activitythread.java:135) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 04-23 05:15:18.769: e/windowmanager(1398): @ android.os.handler.dispatchmessage(handler.java:102) 04-23 05:15:18.769: e/windowmanager(1398): @ android.os.looper.loop(looper.java:136) 04-23 05:15:18.769: e/windowmanager(1398): @ android.app.activitythread.main(activitythread.java:5017) 04-23 05:15:18.769: e/windowmanager(1398): @ java.lang.reflect.method.invokenative(native method) 04-23 05:15:18.769: e/windowmanager(1398): @ java.lang.reflect.method.invoke(method.java:515) 04-23 05:15:18.769: e/windowmanager(1398): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 04-23 05:15:18.769: e/windowmanager(1398): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 04-23 05:15:18.769: e/windowmanager(1398): @ dalvik.system.nativestart.main(native method) 04-23 05:15:18.769: i/choreographer(1398): skipped 96 frames! application may doing work on main thread. 04-23 05:15:18.849: i/choreographer(1398): skipped 46 frames! application may doing work on main thread. 04-23 05:15:20.819: i/process(1398): sending signal. pid: 1398 sig: 9
follow caused by:
java.lang.nullpointerexception in 04-23 05:15:15.839: e/androidruntime(1398): @ com.ambrogroup.foodview$loadallproducts.doinbackground(foodview.java:129)
debug code , try print response json, might have null trying access.
ps if actual line numbers same snippet, noted
foodview.java:129 has null accessing
mstrings[i] = img;
and haven't initialized mstrings array, need add line
string[] mstrings= new string[products.length()];
before of for-loop, before assigning values mstrings array
cheers
Comments
Post a Comment