3

I'm trying to populate custom ListView with this LazyAdapter class from this tutorial

I want to make list view items with custom design. Layout file list_row.xml is in my layouts file just like in the tutorial. This is a slightly modified getView function in LazyAdapter class:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        //vi = inflater.inflate(R.layout.list_row, null);    //EDITED
        vi = inflater.inflate(R.layout.list_row, parent, false);
    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // artist name
    //ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

    //This is not needed
    //HashMap<String, String> post = new HashMap<String, String>();
    HashMap<String, String> post;
    post = data.get(position);
    // Setting all values in listview
    title.setText(post.get("title"));     //the code crashes here
    subtitle.setText(post.get("subtitle"));
    //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

Well this code crashes at title.setText(), because the title is null. I read somewhere that this makes sense because this LazyAdapter is calling findViewById before setContentView or something..

So how do I have to do this? And how does this code work for that guy?

Update

This is the list_row.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="14sp"
        android:textStyle="bold"/>

    <!-- Subtitle -->
    <TextView
        android:id="@+id/subtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/subtitle"
        android:textColor="#343434"
        android:textSize="10sp"
        android:layout_marginTop="1dip" />

</RelativeLayout>

To save space I'm only adding ListView xml from the main activity layout file:

<ListView
    android:id="@+id/feedListView"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/volumeControl1">
</ListView>

Adapter code: http://pastebin.com/niC1yXiJ

And the stack trace: http://pastebin.com/myW8B0Xz

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
user568021
  • 1,426
  • 5
  • 28
  • 55
  • setContentView is used by Activities, and not used in this case. If the inflater is truly inflating a layout with the ids you are referencing, there should not be a null pointer. Could you post the xml that goes along with this? Also, you should be calling inflater.inflate(R.layout.list_row, parent, false); This allows the inflated layout to use the parent's layout but the false keeps it from getting attached. You might also post the error log – Spidy Dec 03 '13 at 04:57
  • 1
    What is data here below the hashmap code line ? If you are getting null at title then there is no value by key title in post hashmap. – Rahul Gupta Dec 03 '13 at 04:58
  • HashMap data gets populated.. that's working ok.. – user568021 Dec 03 '13 at 05:02
  • post stack trace too. – RobinHood Dec 03 '13 at 05:10
  • Ignore Sathish's comment, this isn't a duck typed language. You'd be better off with String.format("%1$s", post.get("title")) – Spidy Dec 03 '13 at 05:17
  • No. I checked with the debugger: findViewById returns null so title is null. There's no problem with data. – user568021 Dec 03 '13 at 05:19
  • If findViewById is returning null, then you are loading the wrong xml or have caching issues. Try changing your inflate method like I said in my first comment. – Spidy Dec 03 '13 at 05:21
  • Have you tried changing the inflate parameters? – Spidy Dec 03 '13 at 05:35
  • can u post ur whole adapter – Kaushik Dec 03 '13 at 05:39
  • yesterday I gave up.. it was 6 o'clock in the morning.. :| I'm still trying to resolve this.. – user568021 Dec 03 '13 at 21:39

3 Answers3

1

Try passing in the parent when you inflate your cell. Using this method:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

vi = inflater.inflate(R.layout.list_row, parent, false);
sgarman
  • 6,152
  • 5
  • 40
  • 44
  • yes. that's exactly what i have now but doesn't solve the problem.. I'll edit the code in the question... – user568021 Dec 03 '13 at 06:24
  • Can you just for testing purposes remove your hashmap and hardcode in values so I can be sure of what is actually null in this situation, example: title.setText("test"); – sgarman Dec 03 '13 at 22:04
  • I get the same error. This is the new getView function http://pastebin.com/CyDBW7dx and this is the new stack trace: http://pastebin.com/97hCnLQY – user568021 Dec 03 '13 at 22:23
1

This was a hell of a ride (two sleepless nights). Thanks for all your help, somehow I fixed it! :)

Mostly thanks to this question Null pointer exception in getView() of custom Adapter I made new getView function:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View vi = convertView;
    if (vi == null) {
        LayoutInflater inflater = ((Activity)activity).getLayoutInflater();
        vi = inflater.inflate(R.layout.list_row, null);
        holder.title = (TextView) vi.findViewById(R.id.title);
        holder.subtitle = (TextView) vi.findViewById(R.id.subtitle);
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }
    HashMap<String, String> post;
    post = data.get(position);
    holder.title.setText(post.get("title"));
    holder.subtitle.setText(post.get("subtitle"));
    return vi;
}

holder is just a simple class:

private class ViewHolder {
    public TextView title;
    public TextView subtitle;
}

I've done that and I also copied xml drawable files in all drawable folders (merged all). So one of this actions fixed it, but I'm not quite sure what..

Community
  • 1
  • 1
user568021
  • 1,426
  • 5
  • 28
  • 55
0

If you get android.widget.AbsListView.obtainView(AbsListView.java:1408) exception than your returned View of getView() is probably null.So check first of all that you are not return null view.

kiran boghra
  • 3,662
  • 2
  • 18
  • 23