开发者

findviewbyid() returns null in custom Adapter in Eclipse

Welcome to another findviewbyid() returns null question!

I wrote an adapter class for displaying an array into a ListView. And gues what findviewbyid() returns null. (I checked all the other questions on this topic but they're not related to my problem).

So here is my Adapter Class:

package de.kosmowski.discogs.wants;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.kosmowski.discogs.model.Want;

public class WantAdapter extends ArrayAdapter<Want> {

private ArrayList<Want> items;

public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
        super(context, textViewResourceId, items);
        this.items = items;
}


public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;    

        Want w = items.get(position);
        Log.d("WantAdapter", "want:" + w);

        if (v == null) {
             LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
             {
                 Log.d(this.getClass().ge开发者_开发问答tCanonicalName(), "Choosing Release List Item");
                 v = vi.inflate(R.layout.releaselistitem, null);
             }
             else
             {
                 v = vi.inflate(R.layout.lplistitem, null);
             }

        }

        if (w != null) {

                ImageView icon = (ImageView) v.findViewById(R.id.typeicon); //Returns NULL at this line

                TextView tt = (TextView) v.findViewById(R.id.text1);

                if(w.getType() != null)
                {
                    if(w.getType().equals(Want.TYPE_ARTIST))
                    {
                        icon.setImageResource(R.drawable.resulttype_artist);
                    }
                    else if(w.getType().equals(Want.TYPE_RELEASE))
                    {
                        icon.setImageResource(R.drawable.resulttype_lp);
                    }
                    else if(w.getType().equals(Want.TYPE_MASTER))
                    {
                        icon.setImageResource(R.drawable.resulttype_lp_master);
                    }
                    else
                    {
                        icon.setImageResource(R.drawable.defaultresult);
                    }
                }
                else
                {
                    icon.setImageResource(R.drawable.defaultresult);
                }



                if (tt != null) {
                      tt.setText(w.getName());                            }                    
        }
        return v;
}


}

I marked the line where null is returned.

And here is the inflated xml file (both the lplistitem and the releaselistitem are currently exactly the same):

<?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">

  <ImageView android:scaleType="center" android:layout_gravity="center_vertical" android:src="@drawable/defaultresult" android:id="@+id/typeicon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:textSize="16px"
    android:minHeight="?android:attr/listPreferredItemHeight"
/> 


</LinearLayout>

Can you see any obvious errors? Everything seems right to me.


So the problem was really related to eclipse. After i did everything you mentioned and googled my hands to the bone i thought it might be a good idea to check if eclipse is doing right.

So for some reason the layout file eclipse displayed to me was not the file which was getting compiled. So for that reason everything looked fine in eclipse.

I then had a look at the same file directly in the File System and this was still the old one. So i just refreshed my eclipse project and voila the old file showed up in eclipse and i edited it again. That was ist. I don't know why this happend and this scares my there could be more of those untransparent problems with eclipse.

I had a problem a few days ago that was kind of similar to it.

see RuntimeException with Android OptionsMenu

Where the problem was, that the R.java file was not recompiled automaticly after adding a new id resource.

I'm going to clean up my eclipse installation now and install everything from the scratch just to make sure there was no major mistake happened during my installation.

Thanks for your answers!


Try terminating the ImageView XML entry with /> instead of ...>< / ImageView>

If not that, then try removing each of the attributes one-by-one (like layout_gravity, etc)

Good luck.


I would be tempted to not try and have a LayoutInflater variable but try to inflate the layout in one line. Also i would be tempted to store the Context as a private variable to ensure the right context is being used. So heres what i would try doing differently in your code:

public class WantAdapter extends ArrayAdapter<Want> {

  private ArrayList<Want> items;
  private Context mContext;

public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
        super(context, textViewResourceId, items);
        this.items = items;
        mContext = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;    

    Want w = items.get(position);
    Log.d("WantAdapter", "want:" + w);

    if (v == null) {
         if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
         {
             Log.d(this.getClass().getCanonicalName(), "Choosing Release List Item");
             v = LayoutInflater.from(mContext).inflate(R.layout.releaselistitem, null);
         }
         else
         {
             v = LayoutInflater.from(mContext).inflate(R.layout.lplistitem, null);
         }

    }

I would keep the rest of the code below these lines the same. Give that a try!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜