java - listview crash after i put in cutom arrayadapter -
i start program android , want make program little more complex, practice.
i run on problem related how java super classes work , can not debug know @ moment.
i wrote code work perfectly, when put listview in default arrayadapter , try make custom arrayadapter crash try refresh listview. here part of code related custom adapter:
private button buttonnovoime; private listview lvradnici; private arraylist <string> listaradnikaarray; private stablearrayadapter adapter; private edittext novoime; private textview buferzaime; private textview errortext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_organizator_smena); buttonnovoime= (button) findviewbyid(r.id.buttonnovoime); lvradnici = (listview) findviewbyid(r.id.listviewradnici); listaradnikaarray = new arraylist<string>(); novoime = (edittext) findviewbyid(r.id.edittextnovoime); errortext= (textview) findviewbyid(r.id.errortext); final stablearrayadapter adapter = new stablearrayadapter (this, r.layout.listview_podesavanje , listaradnikaarray); lvradnici.setadapter(adapter); buttonnovoime.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (listaradnikaarray.contains(novoime.gettext().tostring()) || novoime.gettext().tostring().matches("")) { if (novoime.gettext().tostring().matches("")) { errortext.settext("prazno"); } else { errortext.settext("duplikat"); } } else { listaradnikaarray.add(novoime.gettext().tostring()); adapter.notifydatasetchanged(); errortext.settext(""); } novoime.settext(""); } });
and here custom arrayadapter, standard make:
private class stablearrayadapter extends arrayadapter<string> { hashmap<string, integer> midmap = new hashmap<string, integer>(); public stablearrayadapter(context context, int textviewresourceid, list<string> objects) { super(context, textviewresourceid, objects); (int = 0; < objects.size(); ++i) { midmap.put(objects.get(i), i); } } @override public long getitemid(int position) { string item = getitem(position); return midmap.get(item); } @override public boolean hasstableids() { return true; } }
here error log
01-31 15:04:06.385: e/androidruntime(858): fatal exception: main 01-31 15:04:06.385: e/androidruntime(858): java.lang.nullpointerexception 01-31 15:04:06.385: e/androidruntime(858): @ com.example.organizatorsmena.organizatorsmena$stablearrayadapter.getitemid(organizatorsmena.java:263) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.abslistview.obtainview(abslistview.java:2292) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.listview.measureheightofchildren(listview.java:1244) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.listview.onmeasure(listview.java:1156) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.view.measure(view.java:15172) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:617) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.relativelayout.onmeasure(relativelayout.java:399) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.view.measure(view.java:15172) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4816) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.framelayout.onmeasure(framelayout.java:310) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.view.measure(view.java:15172) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.linearlayout.measurevertical(linearlayout.java:833) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.linearlayout.onmeasure(linearlayout.java:574) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.view.measure(view.java:15172) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4816) 01-31 15:04:06.385: e/androidruntime(858): @ android.widget.framelayout.onmeasure(framelayout.java:310) 01-31 15:04:06.385: e/androidruntime(858): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2148) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.view.measure(view.java:15172) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1850) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1102) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1275) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1000) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4214) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.choreographer$callbackrecord.run(choreographer.java:725) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.choreographer.docallbacks(choreographer.java:555) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.choreographer.doframe(choreographer.java:525) 01-31 15:04:06.385: e/androidruntime(858): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:711) 01-31 15:04:06.385: e/androidruntime(858): @ android.os.handler.handlecallback(handler.java:615) 01-31 15:04:06.385: e/androidruntime(858): @ android.os.handler.dispatchmessage(handler.java:92) 01-31 15:04:06.385: e/androidruntime(858): @ android.os.looper.loop(looper.java:137) 01-31 15:04:06.385: e/androidruntime(858): @ android.app.activitythread.main(activitythread.java:4745) 01-31 15:04:06.385: e/androidruntime(858): @ java.lang.reflect.method.invokenative(native method) 01-31 15:04:06.385: e/androidruntime(858): @ java.lang.reflect.method.invoke(method.java:511) 01-31 15:04:06.385: e/androidruntime(858): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 01-31 15:04:06.385: e/androidruntime(858): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 01-31 15:04:06.385: e/androidruntime(858): @ dalvik.system.nativestart.main(native method)
thanks.
you creating empty arraylist, providing adapter :
listaradnikaarray = new arraylist<string>(); // empty novoime = (edittext) findviewbyid(r.id.edittextnovoime); errortext= (textview) findviewbyid(r.id.errortext); final stablearrayadapter adapter = new stablearrayadapter (this, r.layout.listview_podesavanje , listaradnikaarray);
you need put items in arraylist before using in adapter.
Comments
Post a Comment