NullPointerException in getView in ListView with multiple views in Android -
i know there lots , lots of questions 1 i'm going ask, however, none of them can give me solution. i've read blogs , questions here, no luck. have listview in android, , want display news 2 types of views/layouts: 1 news have pictures, , 1 news don't have (only text),so app can display this. everytime run app getting error:
e/androidruntime(3973): fatal exception: main e/androidruntime(3973): process: com.example.test23, pid: 3973 e/androidruntime(3973): java.lang.nullpointerexception e/androidruntime(3973): @ com.example.test23.itemadapter.getview(itemadapter.java:80) e/androidruntime(3973): @ android.widget.abslistview.obtainview(abslistview.java:2263) e/androidruntime(3973): @ android.widget.listview.measureheightofchildren(listview.java:1263)
i implementing methods getitemviewtype
, getviewtypecount()
. i've implemented famous viewholder
no luck either. right adapter class (cause i've changed plenty of times):
public class itemadapter extends baseadapter implements onitemclicklistener { item item = null; private arraylist<item> news = new arraylist<item>(); private static final int no_picture_view = 0; private static final int picture_view = 1; public itemadapter(context context, arraylist<item> items) { super(); news.addall(items); } @override public int getcount() { return news.size(); } @override public item getitem(int index) { return getitem(index); } @override public long getitemid(int index) { return index; } public int getviewtypecount() { return 2; } public int getitemviewtype(int position) { item article = news.get(position); if (article.getimageurl() != null && !article.getimageurl().equals("") && article.getimageurl().indexof("no_pic.png") == -1) // if there's no pic ? // type_separator // : // type_item; return no_picture_view; else return picture_view; } @override public view getview(int index, view view, viewgroup parent) { item = news.get(index); int type = getitemviewtype(index); int resource; if (type == no_picture_view) { resource = r.layout.item_no_picture_list; } else { resource = r.layout.item_picture_list; } if (view == null) { layoutinflater inflater = layoutinflater.from(parent.getcontext()); view = inflater.inflate(resource, parent, false); textview titleview = (textview) view .findviewbyid(r.id.titlearticle); titleview.settext(item.gettitle());// line gives me // nullpointerexception } return view; } @override public void onitemclick(...) { } }
the layouts of both views ok, i've tested them , no problem. don't want separator, i've seen on other blogs, want able display both view same type of information, array. if need more info or more pieces of code, let me know. or info appreciated. in advance.
update here 2 layouts, new pics:
<imageview android:id="@+id/imageitem" android:layout_width="80dp" android:layout_height="80dp" android:adjustviewbounds="true" android:scaletype="fitxy" android:contentdescription="descripciĆ³n del contenido de la imagen" android:src="@drawable/rr_logo"/> <linearlayout android:layout_width="fill_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <textview android:id="@+id/titlearticle" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:textstyle="bold" android:textsize="18sp" android:textcolor="#000000" android:layout_margintop="5dp" android:layout_marginbottom="5dp"/> </linearlayout> </linearlayout>
for news no pics:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/itemnopicture" > <textview android:id="@+id/titlenopicture" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="18dp"/> </linearlayout>
there 2 reasons error
1) in either 1 of layout files (item_no_picture_list
or item_picture_list
) there no child id titlearticle
2) item in arraylist news @ index index
null
check whichever case.. can use logs.. write below code above line causing error..
if(item == null) log.i("whoisnull", "item"); if(titleview == null) log.i("whoisnull", "titleview");
Comments
Post a Comment