android - created dynamic lazylist with image loader -
some body me..
it's logcat :
03-10 14:53:02.843: e/androidruntime(1754): fatal exception: main 03-10 14:53:02.843: e/androidruntime(1754): java.lang.runtimeexception: unable instantiate activity componentinfo{com.dharma.aplikasilokomedia/com.dharma.aplikasilokomedia.detailloko}: java.lang.nullpointerexception 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.activitythread.performlaunchactivity(activitythread.java:2106) 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.activitythread.access$600(activitythread.java:141) 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 03-10 14:53:02.843: e/androidruntime(1754): @ android.os.handler.dispatchmessage(handler.java:99) 03-10 14:53:02.843: e/androidruntime(1754): @ android.os.looper.loop(looper.java:137) 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.activitythread.main(activitythread.java:5041) 03-10 14:53:02.843: e/androidruntime(1754): @ java.lang.reflect.method.invokenative(native method) 03-10 14:53:02.843: e/androidruntime(1754): @ java.lang.reflect.method.invoke(method.java:511) 03-10 14:53:02.843: e/androidruntime(1754): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 03-10 14:53:02.843: e/androidruntime(1754): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 03-10 14:53:02.843: e/androidruntime(1754): @ dalvik.system.nativestart.main(native method) 03-10 14:53:02.843: e/androidruntime(1754): caused by: java.lang.nullpointerexception 03-10 14:53:02.843: e/androidruntime(1754): @ com.dharma.aplikasilokomedia.filecache.<init>(filecache.java:17) 03-10 14:53:02.843: e/androidruntime(1754): @ com.dharma.aplikasilokomedia.imageloader.<init>(imageloader.java:32) 03-10 14:53:02.843: e/androidruntime(1754): @ com.dharma.aplikasilokomedia.detailloko.<init>(detailloko.java:28) 03-10 14:53:02.843: e/androidruntime(1754): @ java.lang.class.newinstanceimpl(native method) 03-10 14:53:02.843: e/androidruntime(1754): @ java.lang.class.newinstance(class.java:1319) 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.instrumentation.newactivity(instrumentation.java:1054) 03-10 14:53:02.843: e/androidruntime(1754): @ android.app.activitythread.performlaunchactivity(activitythread.java:2097) 03-10 14:53:02.843: e/androidruntime(1754): ... 11 more
detailloko.java :
public class detailloko extends activity { public imageloader imageloader; { imageloader = new imageloader(null); } jsonarray string_json = null; string idberita; private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); public static final string tag_id = "id"; public static final string tag_judul = "judul"; public static final string tag_gambar = "gambar"; private static final string url_detail_berita = "http://10.0.2.2/lokomedia/lokoandro/detailberita.php"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.single_list_item); intent = getintent(); idberita = i.getstringextra(tag_id); new ambildetailberita().execute(); } class ambildetailberita extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(detailloko.this); pdialog.setmessage("mohon tunggu ... !"); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... params) { try { list<namevaluepair> params1 = new arraylist<namevaluepair>(); params1.add(new basicnamevaluepair("idberita",idberita)); jsonobject json = jsonparser.makehttprequest( url_detail_berita, "get", params1); string_json = json.getjsonarray("berita"); runonuithread(new runnable() { public void run() { imageview thumb_image = (imageview) findviewbyid(r.id.imageview1); textview judul = (textview) findviewbyid(r.id.judul); textview detail = (textview) findviewbyid(r.id.detail); textview isi = (textview) findviewbyid(r.id.content); try { // ambil objek member pertama dari json array jsonobject ar = string_json.getjsonobject(0); string judul_d = ar.getstring("judul"); string detail_d = ar.getstring("hari")+" , "+ar.getstring("tanggal")+" diposting oleh : "+ar.getstring("username"); string isi_d = ar.getstring("isi"); judul.settext(judul_d); detail.settext(detail_d); isi.settext(isi_d); imageloader.displayimage(ar.getstring(tag_gambar),thumb_image); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.detail_menu, menu); return true; } public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.home: // single menu item selected // ex: launching new activity/screen or show alert message finish(); intent = new intent(getapplicationcontext(), lokoutama.class); startactivity(i); return true; case r.id.exit: keluar(); return true; default: return super.onoptionsitemselected(item); } } public void keluar(){ alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("apakah anda ingin" + " keluar?") .setcancelable(false) .setpositivebutton("ya", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { finish(); } }) .setnegativebutton("tidak", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { dialog.cancel(); } }).show(); }
}
filecache.java :
public class filecache { private file cachedir; public filecache(context context) { if (android.os.environment.getexternalstoragestate().equals( android.os.environment.media_mounted)) cachedir = new file(android.os.environment .getexternalstoragedirectory(), "lazylist"); else cachedir = context.getcachedir(); if (!cachedir.exists()) cachedir.mkdirs(); } public file getfile(string url) { string filename = string.valueof(url.hashcode()); file f = new file(cachedir, filename); return f; } public void clear() { file[] files = cachedir.listfiles(); if (files == null) return; (file f : files) f.delete(); }
}
imageloader.java :
public class imageloader {
memorycache memorycache = new memorycache(); filecache filecache; private map<imageview, string> imageviews = collections .synchronizedmap(new weakhashmap<imageview, string>()); executorservice executorservice; public imageloader(context context) { filecache = new filecache(context); executorservice = executors.newfixedthreadpool(5); } final int stub_id = r.drawable.no_image; public void displayimage(string url, imageview imageview) { imageviews.put(imageview, url); bitmap bitmap = memorycache.get(url); if (bitmap != null) imageview.setimagebitmap(bitmap); else { queuephoto(url, imageview); imageview.setimageresource(stub_id); } } private void queuephoto(string url, imageview imageview) { phototoload p = new phototoload(url, imageview); executorservice.submit(new photosloader(p)); } private bitmap getbitmap(string url) { file f = filecache.getfile(url); bitmap b = decodefile(f); if (b != null) return b; try { bitmap bitmap = null; url imageurl = new url(url); httpurlconnection conn = (httpurlconnection) imageurl .openconnection(); conn.setconnecttimeout(30000); conn.setreadtimeout(30000); conn.setinstancefollowredirects(true); inputstream = conn.getinputstream(); outputstream os = new fileoutputstream(f); utils.copystream(is, os); os.close(); bitmap = decodefile(f); return bitmap; } catch (exception ex) { ex.printstacktrace(); return null; } } private bitmap decodefile(file f) { try { bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f), null, o); final int required_size = 70; int width_tmp = o.outwidth, height_tmp = o.outheight; int scale = 1; while (true) { if (width_tmp / 2 < required_size || height_tmp / 2 < required_size) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; return bitmapfactory.decodestream(new fileinputstream(f), null, o2); } catch (filenotfoundexception e) { } return null; } private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i) { url = u; imageview = i; } } class photosloader implements runnable { phototoload phototoload; photosloader(phototoload phototoload) { this.phototoload = phototoload; } @override public void run() { if (imageviewreused(phototoload)) return; bitmap bmp = getbitmap(phototoload.url); memorycache.put(phototoload.url, bmp); if (imageviewreused(phototoload)) return; bitmapdisplayer bd = new bitmapdisplayer(bmp, phototoload); activity = (activity) phototoload.imageview.getcontext(); a.runonuithread(bd); } } boolean imageviewreused(phototoload phototoload) { string tag = imageviews.get(phototoload.imageview); if (tag == null || !tag.equals(phototoload.url)) return true; return false; } class bitmapdisplayer implements runnable { bitmap bitmap; phototoload phototoload; public bitmapdisplayer(bitmap b, phototoload p) { bitmap = b; phototoload = p; } public void run() { if (imageviewreused(phototoload)) return; if (bitmap != null) phototoload.imageview.setimagebitmap(bitmap); else phototoload.imageview.setimageresource(stub_id); } } public void clearcache() { memorycache.clear(); filecache.clear(); }
}
use in postexecute() judul.settext(judul_d); detail.settext(detail_d); isi.settext(isi_d); imageloader.displayimage(ar.getstring(tag_gambar),thumb_image);
Comments
Post a Comment