android - Adapter error while running code -
i new android development , working on listview .. code not have error when try run it, application crashes.
this mainactivity code:
package com.example.view; import java.util.arraylist; import android.app.activity; import android.os.bundle; import android.widget.listview; public class mainactivity extends activity { listview lv; arraylist<listviewitem> items; customlistviewadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lv = (listview) findviewbyid(r.id.listview); items = new arraylist<mainactivity.listviewitem>(); items.add(new listviewitem(r.drawable.ic_launcher, "item1", "item1 description")); items.add(new listviewitem(r.drawable.ic_launcher, "item2", "item2 description")); items.add(new listviewitem(r.drawable.ic_launcher, "item3", "item3 description")); items.add(new listviewitem(r.drawable.ic_launcher, "item4", "item4 description")); adapter = new customlistviewadapter(this, items); lv.setadapter(adapter); } class listviewitem { private int thumbnailresource; private string title; private string subtitle; public listviewitem() { } public listviewitem(int thumbnailresource, string title, string subtitle) { this.thumbnailresource = thumbnailresource; this.title = title; this.subtitle = subtitle; } public int getthumbnailresource() { return thumbnailresource; } public void setthumbnailresource(int thumbnailresource) { this.thumbnailresource = thumbnailresource; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getsubtitle() { return subtitle; } public void setsubtitle(string subtitle) { this.subtitle = subtitle; } } }
and adapter class:
package com.example.view; import java.util.arraylist; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.imageview; import android.widget.textview; import com.example.view.mainactivity.listviewitem; public class customlistviewadapter extends arrayadapter<listviewitem> { layoutinflater inflater; arraylist<listviewitem> items; public customlistviewadapter(activity context, arraylist<listviewitem> items) { super(context, r.layout.list_item_row, items); this.items = items; this.inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); } public view getview(final int position, view convertview, viewgroup parent) { listviewitem item = items.get(position); view rowview = inflater.inflate(r.layout.list_item_row, parent, false); imageview imgview = (imageview) rowview.findviewbyid(r.id.imgthumbnail); textview txttitle = (textview) rowview.findviewbyid(r.id.txttitle); textview txtsubtitle = (textview) rowview.findviewbyid(r.id.txtsubtitle); checkbox chkbox = (checkbox) rowview.findviewbyid(r.id.checkbox); imgview.setimageresource(item.getthumbnailresource()); txttitle.settext(item.gettitle()); txtsubtitle.settext(item.getsubtitle()); return rowview; } }
and xml file row
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" > <imageview android:layout_width="78dip" android:layout_height="78dip" android:id="@+id/imgthumbnail" android:layout_alignparentleft="true" android:layout_centerinparent="true" android:layout_marginleft="-3dip" android:scaletype="centerinside"> </imageview> <textview android:textappearance="?android:attr/textappearancelarge" android:layout_height="wrap_content" android:text="textview" android:layout_width="wrap_content" android:id="@+id/txttitle" android:layout_torightof="@+id/imgthumbnail" android:layout_margintop="6dip" android:layout_marginleft="6dip"> </textview> <textview android:layout_height="wrap_content" android:text="textview" android:layout_width="wrap_content" android:id="@+id/txtsubtitle" android:layout_torightof="@+id/imgthumbnail" android:layout_below="@+id/txttitle" android:layout_margintop="3dip" android:layout_marginleft="6dip"> </textview> <listview android1:id="@+id/listview" android1:layout_width="match_parent" android1:layout_height="wrap_content" android1:layout_alignleft="@+id/imgthumbnail" android1:layout_aligntop="@+id/txttitle" android1:layout_marginleft="17dp" > </listview> <checkbox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_marginleft="4px" android:layout_marginright="10px" /> </relativelayout>
and logcat errors
06-27 14:59:38.748: e/moreinfohpw_viewgroup(11621): parent view not textview 06-27 14:59:38.773: e/androidruntime(11621): fatal exception: main 06-27 14:59:38.773: e/androidruntime(11621): process: com.example.view, pid: 11621 06-27 14:59:38.773: e/androidruntime(11621): java.lang.runtimeexception: unable start activity componentinfo{com.example.view/com.example.view.mainactivity}: java.lang.nullpointerexception 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activitythread.performlaunchactivity(activitythread.java:2282) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2340) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activitythread.access$800(activitythread.java:157) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activitythread$h.handlemessage(activitythread.java:1247) 06-27 14:59:38.773: e/androidruntime(11621): @ android.os.handler.dispatchmessage(handler.java:102) 06-27 14:59:38.773: e/androidruntime(11621): @ android.os.looper.loop(looper.java:157) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activitythread.main(activitythread.java:5293) 06-27 14:59:38.773: e/androidruntime(11621): @ java.lang.reflect.method.invokenative(native method) 06-27 14:59:38.773: e/androidruntime(11621): @ java.lang.reflect.method.invoke(method.java:515) 06-27 14:59:38.773: e/androidruntime(11621): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 06-27 14:59:38.773: e/androidruntime(11621): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 06-27 14:59:38.773: e/androidruntime(11621): @ dalvik.system.nativestart.main(native method) 06-27 14:59:38.773: e/androidruntime(11621): caused by: java.lang.nullpointerexception 06-27 14:59:38.773: e/androidruntime(11621): @ com.example.view.mainactivity.oncreate(mainactivity.java:27) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activity.performcreate(activity.java:5389) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 06-27 14:59:38.773: e/androidruntime(11621): @ android.app.activitythread.performlaunchactivity(activitythread.java:2246) 06-27 14:59:38.773: e/androidruntime(11621): ... 11 more
you getting error on line lv.setadapter(adapter);
i sure listview
not present in activity_main.xml layout file.
you have put listview
in activity_main.xml
file.
you added listview
mistake in layout of row file.
Comments
Post a Comment