java - App crashes when trying to implement a custom list Adapter ( NullPointerException ) -
error:
05-17 11:27:39.986: e/androidruntime(5172): fatal exception: main 05-17 11:27:39.986: e/androidruntime(5172): process: com.example.yevsnap, pid: 5172 05-17 11:27:39.986: e/androidruntime(5172): java.lang.nullpointerexception 05-17 11:27:39.986: e/androidruntime(5172): @ com.example.yevsnap.messageadapter.getview(messageadapter.java:46) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.abslistview.obtainview(abslistview.java:2240) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.listview.makeandaddview(listview.java:1790) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.listview.filldown(listview.java:691) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.listview.fillfromtop(listview.java:752) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.listview.layoutchildren(listview.java:1630) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.abslistview.onlayout(abslistview.java:2091) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.relativelayout.onlayout(relativelayout.java:1055) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.framelayout.layoutchildren(framelayout.java:453) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.framelayout.onlayout(framelayout.java:388) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) 05-17 11:27:39.986: e/androidruntime(5172): @ android.support.v4.view.viewpager.onlayout(viewpager.java:1589) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.framelayout.layoutchildren(framelayout.java:453) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.framelayout.onlayout(framelayout.java:388) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) 05-17 11:27:39.986: e/androidruntime(5172): @ com.android.internal.widget.actionbaroverlaylayout.onlayout(actionbaroverlaylayout.java:374) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.framelayout.layoutchildren(framelayout.java:453) 05-17 11:27:39.986: e/androidruntime(5172): @ android.widget.framelayout.onlayout(framelayout.java:388) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.view.layout(view.java:14817) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewgroup.layout(viewgroup.java:4631) -17 11:27:39.986: e/androidruntime(5172): @ android.view.viewrootimpl.performlayout(viewrootimpl.java:1987) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1744) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1000) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5670) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.choreographer$callbackrecord.run(choreographer.java:761) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.choreographer.docallbacks(choreographer.java:574) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.choreographer.doframe(choreographer.java:544) 05-17 11:27:39.986: e/androidruntime(5172): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:747) 05-17 11:27:39.986: e/androidruntime(5172): @ android.os.handler.handlecallback(handler.java:733) -17 11:27:39.986: e/androidruntime(5172): @ android.os.handler.dispatchmessage(handler.java:95) 05-17 11:27:39.986: e/androidruntime(5172): @ android.os.looper.loop(looper.java:136) 05-17 11:27:39.986: e/androidruntime(5172): @ android.app.activitythread.main(activitythread.java:5017) 05-17 11:27:39.986: e/androidruntime(5172): @ java.lang.reflect.method.invokenative(native method) 05-17 11:27:39.986: e/androidruntime(5172): @ java.lang.reflect.method.invoke(method.java:515) 05-17 11:27:39.986: e/androidruntime(5172): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-17 11:27:39.986: e/androidruntime(5172): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-17 11:27:39.986: e/androidruntime(5172): @ dalvik.system.nativestart.main(native method)
im pretty sure code right:
main part of presenting list fragment:
@override public void onresume() { super.onresume(); getactivity().setprogressbarindeterminatevisibility(true); parsequery<parseobject> query = new parsequery<parseobject>( parseconstants.class_messages); query.whereequalto(parseconstants.key_recipient_ids, parseuser .getcurrentuser().getobjectid()); query.adddescendingorder(parseconstants.key_created_at); query.findinbackground(new findcallback<parseobject>() { @override public void done(list<parseobject> messages, parseexception e) { getactivity().setprogressbarindeterminatevisibility(false); if (e == null) { // found messages! mmessages = messages; string[] usernames = new string[mmessages.size()]; int = 0; (parseobject message : mmessages) { usernames[i] = message .getstring(parseconstants.key_sender_name); i++; } //simple list works: //arrayadapter<string> adapter = new arrayadapter<string>(getlistview().getcontext(), android.r.layout.simple_list_item_1, usernames); //this crashes: messageadapter adapter = new messageadapter(getlistview() .getcontext(), mmessages); setlistadapter(adapter); } } }); }
and custom arrayadapter class. messageadapter.java:
package com.example.yevsnap; import java.util.list; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import com.parse.parseobject; public class messageadapter extends arrayadapter<parseobject> { protected context mcontext; protected list<parseobject> mmessages; public messageadapter(context context, list<parseobject> messages) { super(context, r.layout.message_item, messages); mcontext = context; mmessages = messages; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = layoutinflater.from(mcontext).inflate( r.layout.message_item, null); holder = new viewholder(); holder.iconimageview = (imageview) convertview .findviewbyid(r.id.messageicon); holder.namelabel = (textview) convertview .findviewbyid(r.id.senderlabel); } else { holder = (viewholder) convertview.gettag(); } parseobject message = mmessages.get(position); if (message.getstring(parseconstants.key_file_type).equals( parseconstants.type_image)) { holder.iconimageview.setimageresource(r.drawable.ic_action_picture); } else { holder.iconimageview .setimageresource(r.drawable.ic_action_play_over_video); } holder.namelabel.settext(message .getstring(parseconstants.key_sender_name)); return convertview; } private static class viewholder { imageview iconimageview; textview namelabel; } }
you've forgotten store holder
view. null
returned when attempting retrieve holder when convertview
given recycling.
add
convertview.settag(holder);
to block instantiate holder.
Comments
Post a Comment