java - ImageView in a List Null Pointer Exception -
i trying set list view images, ie no text. here files:
//the main file: public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview listview1 = (listview)findviewbyid(r.id.listview1); final integer[] mthumbids = { r.drawable.blue, r.drawable.blue2, r.drawable.blue3, r.drawable.blue4, r.drawable.blue5, r.drawable.blue6 }; listview1.setadapter(new imageadapter(this,mthumbids)); listview1.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { //upon clicking } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } class imageadapter extends arrayadapter<integer> { private context mcontext; integer [] resources; public imageadapter (context c, integer[] resources) { super(c, r.layout.activity_main, resources); //system.out.println("set of image adapter"); mcontext = c; this.resources=resources; for(integer resource:resources){ system.out.println(resource); } } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.activity_main, parent, false); imageview imageview = (imageview) rowview.findviewbyid(r.layout.image_view); imageview.setimageresource(resources[position]); return rowview; } } }
the xml follows:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/export_folder_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="formation name:" android:textappearance="?android:attr/textappearancemedium" /> <listview android:id="@+id/listview1" android:layout_below="@+id/color_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:columnwidth="90dp" android:numcolumns="auto_fit" android:verticalspacing="10dp" android:horizontalspacing="10dp" android:stretchmode="columnwidth" android:gravity="center" android:background="#ff000000" > </listview> </relativelayout>
and finally, 1 defining imageview:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:minheight="64dp"> <imageview android:id="@+id/imageview" android:layout_width="32dp" android:layout_height="32dp" android:layout_alignparentleft="true" android:layout_marginleft="9dp" android:layout_alignparenttop="true"/> </relativelayout>
unfortunately, nullpointerexceptions @ point set image resource on image view. image resources exist- i've made sure. not sure doing wrong, , new android. thank you.
looks inflating wrong xml in getview(),
view rowview = inflater.inflate(r.layout.activity_main, parent, false);
here r.layout.activity_main should replaced xml file contains imageview.
and code, think name of xml file containing imageview is, r.layout.image_view
so, final code should this,
view rowview = inflater.inflate(r.layout.image_view, parent, false); imageview imageview = (imageview) rowview.findviewbyid(r.id.imageview); imageview.setimageresource(resources[position]);
Comments
Post a Comment