android - Reason behind null pointer exception? -
i using pager adapter display images in view pager ,that downloaded bitmapfactory in asynctask.
pageradapter class:
public class pageradapter extends pageradapter { button load_img; imageview imgview; bitmap bitmap; context mcontext; layoutinflater mlayoutinflater; list<string> l = mainactivity.list; imageloader mimageloader; public pageradapter(context context) { mcontext = context; } @override public int getcount() { return 4; } @override public object instantiateitem(viewgroup container, int position) { imageloader mimageloader = imageloader.getinstance(); mlayoutinflater = ((layoutinflater) container.getcontext().getsystemservice(context.layout_inflater_service)); view view = mlayoutinflater.inflate(r.layout.img, container, false); imgview = (imageview) view.findviewbyid(r.id.imageview3); bitmap btp=new loadimage().doinbackground(l.get(position)); imgview.setimagebitmap(btp); container.addview(view); return view; } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((linearlayout) object); } @override public boolean isviewfromobject(view view, object object) { return view == object; } public class loadimage extends asynctask<string, string, bitmap> { progressdialog pdialog = new progressdialog(mcontext); @override protected void onpreexecute() { super.onpreexecute(); pdialog.setmessage("loading image ...."); pdialog.show(); } protected bitmap doinbackground(string... args) { try { bitmap = bitmapfactory.decodestream((inputstream) new url(args[0]).getcontent()); } catch (exception e) { e.printstacktrace(); } return bitmap; } protected void onpostexecute(bitmap image) { if(image != null){ imgview.setimagebitmap(image); pdialog.dismiss(); }else{ pdialog.dismiss(); toast.maketext(mcontext, "image not exist or network error", toast.length_short).show(); } } } }
my logcat:
02-08 18:49:22.554 18345-18345/autogenie.dg10 e/androidruntime: fatal exception: main java.lang.nullpointerexception @ autogenie.dg10.pageradapter$loadimage.doinbackground(pageradapter.java:89) @ autogenie.dg10.pageradapter.instantiateitem(pageradapter.java:54) @ android.support.v4.view.viewpager.addnewitem(viewpager.java:870) @ android.support.v4.view.viewpager.populate(viewpager.java:1020) @ android.support.v4.view.viewpager.populate(viewpager.java:952) @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1474) @ android.view.view.measure(view.java:15518) @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:681) @ android.widget.relativelayout.onmeasure(relativelayout.java:461) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ android.support.v7.widget.contentframelayout.onmeasure(contentframelayout.java:135) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.support.v7.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:391) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) @ android.widget.linearlayout.measurevertical(linearlayout.java:695) @ android.widget.linearlayout.onmeasure(linearlayout.java:588) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2176) @ android.view.view.measure(view.java:15518) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1874) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1089) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1265) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:989) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4351) @ android.view.choreographer$callbackrecord.run(choreographer.java:749) @ android.view.choreographer.docallbacks(choreographer.java:562) @ android.view.choreographer.doframe(choreographer.java:532) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method)
wrong way call asynctask
. don't directly call doinbackground()
.
change
bitmap btp=new loadimage().doinbackground(l.get(position));
to
bitmap btp = new loadimage().execute(l.get(position));
Comments
Post a Comment