android - NPE when instantiating a convertView -
here code create custom view listview
. uses custom view named squareprogressbar
:
package com.example.simpledownloader.adapter; import net.yscs.android.square_progressbar.squareprogressbar; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; import com.example.simpledownloader.r; import com.example.simpledownloader.sharable.sharable; public class taskadapter extends baseadapter { context ctx = null; public taskadapter(context ctx){ this.ctx = ctx; } //-------------------------------------------------------------------------------- @override public int getcount() { return sharable.downloads.size(); } //-------------------------------------------------------------------------------- @override public object getitem(int index) { return sharable.downloads.get(index); } //-------------------------------------------------------------------------------- @override public long getitemid(int id) { return id; } //-------------------------------------------------------------------------------- @override public view getview(int index, view convertview, viewgroup parent) { if(convertview == null){ layoutinflater inflater = layoutinflater.from(ctx); convertview = inflater.inflate(r.layout.task, parent, false); } squareprogressbar taskprogressbar = (squareprogressbar) convertview.findviewbyid(r.id.taskprogressbar); textview statusmsg = (textview) convertview.findviewbyid(r.id.statusmsg); textview name = (textview) convertview.findviewbyid(r.id.taskname); taskprogressbar.setprogress((int)sharable.downloads.get(index).getprogress()); taskprogressbar.setimage(r.drawable.task_image); taskprogressbar.setcolor("#c9c9c9"); taskprogressbar.setwidth(8); name.settext(sharable.downloads.get(index).tostring()); statusmsg.settext(sharable.downloads.get(index).getstatus()); return convertview; } //-------------------------------------------------------------------------------- }
however, npe:
08-18 15:10:56.184: e/androidruntime(5639): fatal exception: main 08-18 15:10:56.184: e/androidruntime(5639): java.lang.nullpointerexception 08-18 15:10:56.184: e/androidruntime(5639): @ net.yscs.android.square_progressbar.squareprogressbar.setopacity(squareprogressbar.java:184) 08-18 15:10:56.184: e/androidruntime(5639): @ net.yscs.android.square_progressbar.squareprogressbar.setprogress(squareprogressbar.java:105) 08-18 15:10:56.184: e/androidruntime(5639): @ com.example.simpledownloader.adapter.taskadapter.getview(taskadapter.java:47) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.abslistview.obtainview(abslistview.java:2159) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.listview.measureheightofchildren(listview.java:1246) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.listview.onmeasure(listview.java:1158) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.view.measure(view.java:15518) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.linearlayout.measurevertical(linearlayout.java:695) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.view.measure(view.java:15518) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.framelayout.onmeasure(framelayout.java:310) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.view.measure(view.java:15518) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.linearlayout.measurevertical(linearlayout.java:847) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.view.measure(view.java:15518) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 08-18 15:10:56.184: e/androidruntime(5639): @ android.widget.framelayout.onmeasure(framelayout.java:310) 08-18 15:10:56.184: e/androidruntime(5639): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2176) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.view.measure(view.java:15518) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1874) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1089) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1265) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:989) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4351) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.choreographer$callbackrecord.run(choreographer.java:749) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.choreographer.docallbacks(choreographer.java:562) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.choreographer.doframe(choreographer.java:532) 08-18 15:10:56.184: e/androidruntime(5639): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) 08-18 15:10:56.184: e/androidruntime(5639): @ android.os.handler.handlecallback(handler.java:725) 08-18 15:10:56.184: e/androidruntime(5639): @ android.os.handler.dispatchmessage(handler.java:92) 08-18 15:10:56.184: e/androidruntime(5639): @ android.os.looper.loop(looper.java:137) 08-18 15:10:56.184: e/androidruntime(5639): @ android.app.activitythread.main(activitythread.java:5041) 08-18 15:10:56.184: e/androidruntime(5639): @ java.lang.reflect.method.invokenative(native method) 08-18 15:10:56.184: e/androidruntime(5639): @ java.lang.reflect.method.invoke(method.java:511) 08-18 15:10:56.184: e/androidruntime(5639): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-18 15:10:56.184: e/androidruntime(5639): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-18 15:10:56.184: e/androidruntime(5639): @ dalvik.system.nativestart.main(native method)
why that?
i guess square progress bar not found.
ctx application context passed class
sharable custom class contains singleton objects shared across activities
instead of:
taskprogressbar.setprogress((int)sharable.downloads.get(index).getprogress()); taskprogressbar.setimage(r.drawable.task_image);
use:
taskprogressbar.setimage(r.drawable.task_image); taskprogressbar.setprogress((int)sharable.downloads.get(index).getprogress());