java - ListView with custom Rows throws InflaterException -
i try build fragment listview inside. on start of activity app crashes inflaterexception.
i have custom adapter should inflate custom row design listview. can please me?
messagelistrow.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/messagelistrow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/border" android:padding="6dp" > <imageview android:id="@+id/icon" android:layout_width="50dip" android:layout_height="50dip" android:src="@drawable/ic_action_mail_blue" android:layout_alignparentleft="true" android:layout_centerinparent="true" android:scaletype="center" android:contentdescription="@string/messageimagedescription"/> <textview android:id="@+id/messagedate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_torightof="@id/icon" android:ellipsize="marquee" android:singleline="true" android:textcolor="#1e1e1e" android:textsize="@dimen/contenttextsize" /> <textview android:id="@+id/messagesnippet" android:textappearance="?android:attr/textappearancelarge" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_torightof="@+id/icon" android:layout_marginleft="2dip" /> </relativelayout>
messagelistitem.java
public class messagelistitem { private string messagesnippet; private int iconid; private string date; public messagelistitem(string messagesnippet, string date) { this.messagesnippet = messagesnippet; this.iconid = r.drawable.ic_action_mail_blue; this.date = date; } public string getmessagesnippet() { return messagesnippet; } public void setmessagesnippet(string messagesnippet) { this.messagesnippet = messagesnippet; } public int geticonid() { return iconid; } public void seticonid(int iconid) { this.iconid = iconid; } public string getmessagedate() { return date; } public void setmessagedate(string status) { this.date = status; } }
messagelistadapter.java
import java.util.arraylist; import java.util.list; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; public class messagelistadapter extends baseadapter { activity context; layoutinflater inflater; arraylist<object> rowitems; public messagelistadapter(activity context, arraylist<object> rowitems) { this.context = context; this.rowitems = rowitems; } @override public int getcount() { return rowitems.size(); } @override public object getitem(int position) { return rowitems.get(position); } @override public long getitemid(int position) { return rowitems.indexof(getitem(position)); } public static class viewholder { imageview icon; textview messagesnippet; textview messagedate; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if(convertview==null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.messagelistrow, null); holder.icon = (imageview) convertview.findviewbyid(r.id.icon); holder.messagesnippet = (textview) convertview.findviewbyid(r.id.messagesnippet); holder.messagedate = (textview) convertview.findviewbyid(r.id.messagedate); convertview.settag(holder); } else holder=(viewholder)convertview.gettag(); messagelistitem ml = (messagelistitem) rowitems.get(position); holder.icon.setimageresource(ml.geticonid()); holder.messagesnippet.settext(ml.getmessagesnippet()); holder.messagedate.settext(ml.getmessagedate()); return convertview; } }
historymessagelistfragment.java
import java.util.arraylist; import java.util.list; import android.app.activity; import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; import android.widget.toast; public class historymessagelistfragment extends fragment{ private onitemselectedlistener listener; string[] messagesnippets; integer iconid; string[] messagedates; arraylist<object> messagelistrowitems; listview messagelistview; messagelistitem item; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.historylist, container, false); preparearraylits(); messagelistview = (listview) view.findviewbyid(r.id.messagelist); messagelistadapter adapter = new messagelistadapter(getactivity(), messagelistrowitems); messagelistview.setadapter(adapter); messagelistview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string item = "hallo"; toast.maketext(getactivity(), item, toast.length_long).show(); } }); return view; } public interface onitemselectedlistener { public void onmessageselected(string link); } @override public void onattach(activity activity) { super.onattach(activity); if (activity instanceof onitemselectedlistener) { listener = (onitemselectedlistener) activity; } else { throw new classcastexception(activity.tostring() + " must implemenet mylistfragment.onitemselectedlistener"); } } public void preparearraylits() { messagelistrowitems = new arraylist<object>(); addobjecttolist(r.drawable.ic_action_mail_blue, getresources().getstringarray(r.array.messagesnippets)[0], getresources().getstringarray(r.id.messagedate)[0]); } // add 1 item array list public void addobjecttolist(int image, string messagesnippet, string messagedate) { item = new messagelistitem(messagesnippet, messagedate); item.seticonid(image); messagelistrowitems.add(item); } // may triggered activity public void updatedetail() { // create fake data string newtime = string.valueof(system.currenttimemillis()); // send data activity listener.onmessageselected(newtime); } }
historymessagelistactivity.java
package o2.telefonica.mma.abhc; import o2.telefonica.mma.abhc.settings.results; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class historymessagelistactivity extends activity implements menuitem.onmenuitemclicklistener, historymessagelistfragment.onitemselectedlistener{ private menuitem action_about; private menuitem action_exit; private menuitem action_settings; private menuitem action_newmessage; private menuitem action_delete; private menuitem action_search; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.history); } @override public void onmessageselected(string link) { historymessagefragment fragment = (historymessagefragment) getfragmentmanager() .findfragmentbyid(r.id.detailfragment); if (fragment != null && fragment.isinlayout()) { fragment.setmessagetext(link); } else { intent intent = new intent(getapplicationcontext(), historymessageactivity.class); intent.putextra(historymessageactivity.extra_url, link); startactivityforresult(intent, results.start_activity); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.history_activity_actions, menu); this.action_about = menu.finditem(r.id.action_about); this.action_about.setonmenuitemclicklistener(this); this.action_exit = menu.finditem(r.id.action_exit); this.action_exit.setonmenuitemclicklistener(this); this.action_settings = menu.finditem(r.id.action_settings); this.action_settings.setonmenuitemclicklistener(this); this.action_newmessage = menu.finditem(r.id.action_newmessage); this.action_newmessage.setonmenuitemclicklistener(this); this.action_delete = menu.finditem(r.id.action_delete); this.action_delete.setonmenuitemclicklistener(this); this.action_search = menu.finditem(r.id.action_search); this.action_search.setonmenuitemclicklistener(this); return super.oncreateoptionsmenu(menu); } @override public boolean onmenuitemclick(menuitem item) { if (item.getitemid() == r.id.action_delete) { //todo delete message return true; } else if (item.getitemid() == r.id.action_newmessage) { //setcontentview(r.layout.history); finish(); //new historycontroller(this, r.layout.history, m); return true; } else if (item.getitemid() == r.id.action_about) { //setcontentview(r.layout.about); startactivityforresult(new intent(this, aboutactivity.class), results.start_activity); //new aboutcontroller(this, r.layout.about, m); return true; } else if (item.getitemid() == r.id.action_settings) { //setcontentview(r.layout.settings); startactivityforresult(new intent(this, settingsactivity.class), results.start_activity); //new settingscontroller(this, r.layout.settings, m); return true; } else if (item.getitemid() == r.id.action_exit) { //todo exit return true; } else if (item.getitemid() == r.id.action_search) { //todo search function return true; } return false; } }
historylist.xml (fragment layout)
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/historyview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="top" android:background="@null" android:gravity="center" android:orientation="vertical" android:paddingbottom="12dp" android:paddingleft="21dp" android:paddingright="21dp" android:paddingtop="12dp" > <textview android:id="@+id/historymessageslistlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:paddingbottom="@dimen/spacinglabeltotext" android:text="@string/historymessageslabel" android:textappearance="?android:attr/textappearancemedium" android:textcolor="#ffffff" android:textsize="@dimen/titletextsize" /> <listview android:id="@+id/messagelist" android:layout_width="450dp" android:layout_height="fill_parent" android:layout_alignparentbottom="true" android:layout_below="@+id/historymessageslistlabel" android:layout_centerhorizontal="true" android:background="@drawable/textfieldshape" android:minwidth="450dp" android:textcolor="#ffffff" > </listview> </relativelayout>
logcat (update)
06-27 21:44:32.685: i/timeline(7980): timeline: activity_idle id: android.os.binderproxy@64b28c80 time:113147909 06-27 21:44:35.137: i/timeline(7980): timeline: activity_launch_request id:o2.telefonica.mma.abhc time:113150356 06-27 21:44:35.197: i/art(7980): gccausebackground partial concurrent mark sweep gc freed 14537(802kb) allocspace objects, 2(32kb) los objects, 6% free, 13mb/14mb, paused 12.451ms total 76.324ms 06-27 21:44:35.708: i/timeline(7980): timeline: activity_idle id: android.os.binderproxy@64b91c10 time:113150926 06-27 21:44:37.109: i/timeline(7980): timeline: activity_launch_request id:o2.telefonica.mma.abhc time:113152336 06-27 21:44:37.149: e/activitythread(7980): failed inflate 06-27 21:44:37.149: e/activitythread(7980): android.view.inflateexception: binary xml file line #8: error inflating class fragment 06-27 21:44:37.149: e/activitythread(7980): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:713) 06-27 21:44:37.149: e/activitythread(7980): @ android.view.layoutinflater.rinflate(layoutinflater.java:755) 06-27 21:44:37.149: e/activitythread(7980): @ android.view.layoutinflater.inflate(layoutinflater.java:492) 06-27 21:44:37.149: e/activitythread(7980): @ android.view.layoutinflater.inflate(layoutinflater.java:397) 06-27 21:44:37.149: e/activitythread(7980): @ android.view.layoutinflater.inflate(layoutinflater.java:353) 06-27 21:44:37.149: e/activitythread(7980): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:343) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activity.setcontentview(activity.java:1929) 06-27 21:44:37.149: e/activitythread(7980): @ o2.telefonica.mma.abhc.historymessagelistactivity.oncreate(historymessagelistactivity.java:23) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activity.performcreate(activity.java:5231) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activitythread.performlaunchactivity(activitythread.java:2167) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2262) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activitythread.access$800(activitythread.java:145) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activitythread$h.handlemessage(activitythread.java:1206) 06-27 21:44:37.149: e/activitythread(7980): @ android.os.handler.dispatchmessage(handler.java:102) 06-27 21:44:37.149: e/activitythread(7980): @ android.os.looper.loop(looper.java:136) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activitythread.main(activitythread.java:5137) 06-27 21:44:37.149: e/activitythread(7980): @ java.lang.reflect.method.invoke(native method) 06-27 21:44:37.149: e/activitythread(7980): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 06-27 21:44:37.149: e/activitythread(7980): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:611) 06-27 21:44:37.149: e/activitythread(7980): caused by: java.lang.nullpointerexception 06-27 21:44:37.149: e/activitythread(7980): @ java.lang.vmclassloader.findloadedclass(native method) 06-27 21:44:37.149: e/activitythread(7980): @ java.lang.classloader.findloadedclass(classloader.java:362) 06-27 21:44:37.149: e/activitythread(7980): @ java.lang.classloader.loadclass(classloader.java:499) 06-27 21:44:37.149: e/activitythread(7980): @ java.lang.classloader.loadclass(classloader.java:469) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.fragment.instantiate(fragment.java:583) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.fragment.instantiate(fragment.java:561) 06-27 21:44:37.149: e/activitythread(7980): @ android.app.activity.oncreateview(activity.java:4778) 06-27 21:44:37.149: e/activitythread(7980): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:689) 06-27 21:44:37.149: e/activitythread(7980): ... 19 more 06-27 21:44:37.149: d/androidruntime(7980): shutting down vm 06-27 21:44:37.159: e/androidruntime(7980): fatal exception: main 06-27 21:44:37.159: e/androidruntime(7980): process: o2.telefonica.mma.abhc, pid: 7980 06-27 21:44:37.159: e/androidruntime(7980): java.lang.runtimeexception: unable start activity componentinfo{o2.telefonica.mma.abhc/o2.telefonica.mma.abhc.historymessagelistactivity}: android.view.inflateexception: binary xml file line #8: error inflating class fragment 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activitythread.performlaunchactivity(activitythread.java:2213) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2262) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activitythread.access$800(activitythread.java:145) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activitythread$h.handlemessage(activitythread.java:1206) 06-27 21:44:37.159: e/androidruntime(7980): @ android.os.handler.dispatchmessage(handler.java:102) 06-27 21:44:37.159: e/androidruntime(7980): @ android.os.looper.loop(looper.java:136) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activitythread.main(activitythread.java:5137) 06-27 21:44:37.159: e/androidruntime(7980): @ java.lang.reflect.method.invoke(native method) 06-27 21:44:37.159: e/androidruntime(7980): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 06-27 21:44:37.159: e/androidruntime(7980): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:611) 06-27 21:44:37.159: e/androidruntime(7980): caused by: android.view.inflateexception: binary xml file line #8: error inflating class fragment 06-27 21:44:37.159: e/androidruntime(7980): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:713) 06-27 21:44:37.159: e/androidruntime(7980): @ android.view.layoutinflater.rinflate(layoutinflater.java:755) 06-27 21:44:37.159: e/androidruntime(7980): @ android.view.layoutinflater.inflate(layoutinflater.java:492) 06-27 21:44:37.159: e/androidruntime(7980): @ android.view.layoutinflater.inflate(layoutinflater.java:397) 06-27 21:44:37.159: e/androidruntime(7980): @ android.view.layoutinflater.inflate(layoutinflater.java:353) 06-27 21:44:37.159: e/androidruntime(7980): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:343) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activity.setcontentview(activity.java:1929) 06-27 21:44:37.159: e/androidruntime(7980): @ o2.telefonica.mma.abhc.historymessagelistactivity.oncreate(historymessagelistactivity.java:23) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activity.performcreate(activity.java:5231) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activitythread.performlaunchactivity(activitythread.java:2167) 06-27 21:44:37.159: e/androidruntime(7980): ... 9 more 06-27 21:44:37.159: e/androidruntime(7980): caused by: java.lang.nullpointerexception 06-27 21:44:37.159: e/androidruntime(7980): @ java.lang.vmclassloader.findloadedclass(native method) 06-27 21:44:37.159: e/androidruntime(7980): @ java.lang.classloader.findloadedclass(classloader.java:362) 06-27 21:44:37.159: e/androidruntime(7980): @ java.lang.classloader.loadclass(classloader.java:499) 06-27 21:44:37.159: e/androidruntime(7980): @ java.lang.classloader.loadclass(classloader.java:469) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.fragment.instantiate(fragment.java:583) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.fragment.instantiate(fragment.java:561) 06-27 21:44:37.159: e/androidruntime(7980): @ android.app.activity.oncreateview(activity.java:4778) 06-27 21:44:37.159: e/androidruntime(7980): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:689) 06-27 21:44:37.159: e/androidruntime(7980): ... 19 more
history.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:baselinealigned="false" > <fragment android:id="@+id/listfragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="top" android:layout_weight="1" /> <fragment android:id="@+id/detailfragment" android:layout_width="0dp" android:layout_weight="2" android:layout_height="match_parent" /> </linearlayout>
if need more information feel free ask.
thanks in advance.
your problem here:
convertview = inflater.inflate(r.layout.messagelistrow, null);
you not initialize inflater
in code
you need:
inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service);
in getview method, getview method must be:
@override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if(convertview==null) { inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); holder = new viewholder(); convertview = inflater.inflate(r.layout.messagelistrow, null); holder.icon = (imageview) convertview.findviewbyid(r.id.icon); holder.messagesnippet = (textview) convertview.findviewbyid(r.id.messagesnippet); holder.messagedate = (textview) convertview.findviewbyid(r.id.messagedate); convertview.settag(holder); } else holder=(viewholder)convertview.gettag(); messagelistitem ml = (messagelistitem) rowitems.get(position); holder.icon.setimageresource(ml.geticonid()); holder.messagesnippet.settext(ml.getmessagesnippet()); holder.messagedate.settext(ml.getmessagedate()); return convertview; }
another problem in history xml file: line 8
binary xml file line #8: error inflating class fragment
you need change fragment
framelayout
Comments
Post a Comment