Android:listview:自定义项:nullpointerexception,findviewbyid返回null

时间:2012-05-22 09:36:48

标签: android listview android-listview nullpointerexception adapter

我一直在谷歌搜索并试图解决这个错误一段时间,我似乎无法找出原因以及如何解决它。

我正在使用customAdapter来填写我的列表视图。我膨胀描述我的listItem的xml。我使用findViewById创建viewHolder对象并在textview中加载。之后,我想设置该textview的文本,但findViewbyid返回一个空值。所以自动解决为nullpointerexception。

适配器:

/** CLASS : Custom Adapter for the listview */
private class CustomAdapter extends ArrayAdapter<Track>{
    private List<Track> items;
    private Context mContext;
    private int layoutResourceId;       
    private ViewHolder mHolder;

    public CustomAdapter(Context context, int layoutId, List<Track> objects) {
        super(context, layoutId, objects);
        layoutResourceId=layoutId;
        items = objects;
        mContext=context;       
    }       

    public View getView(int position, View convertView, ViewGroup parent) {
         if(convertView == null){
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView = inflater.inflate(layoutResourceId, parent, false);
             mHolder = new ViewHolder();
             ****** RETURNS NULL DURING DEBUGGING ******
             mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);
             convertView.setTag(mHolder);
         }else{
             mHolder = (ViewHolder) convertView.getTag();
         }
         Track track = items.get(position);
         ****** ERROR HAPPENS HERE ******
         mHolder.textViewCenter.setText(track.getTrack());
         return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView textViewCenter;
        TextView textViewRight;
    }
}

XML:

?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:orientation="horizontal" >

<ImageView
    android:id="@+id/imageview_list_albumart_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:src="@drawable/ic_music" />

<TextView
    android:id="@+id/textview_list_item_central"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.5"
    android:text="Center"
    android:textSize="20sp" >
</TextView>

<TextView
    android:id="@+id/textview_list_item_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:paddingRight="10dip"
    android:text="Right" />

感谢任何帮助!

5 个答案:

答案 0 :(得分:7)

convertView中搜索您要充气的视图(而不是像您当前那样在当前的Activity布局中):

 mHolder.textViewCenter = (TextView) 
                convertView.findViewById(R.id.textview_list_item_central);

答案 1 :(得分:0)

您正在给布局充气,但在获取布局时不使用它

这样做:

 mHolder.textViewCenter = (TextView)convertView.findViewById(R.id.textview_list_item_central)

答案 2 :(得分:0)

TextView初始化错误

if(convertView == null){
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView = inflater.inflate(layoutResourceId, parent, false);
             mHolder = new ViewHolder();
             ****** RETURNS NULL DURING DEBUGGING ******
             mHolder.textViewCenter = (TextView) convertView .findViewById(R.id.textview_list_item_central); //Change done
             convertView.setTag(mHolder);
         }else{
             mHolder = (ViewHolder) convertView.getTag();
         }

答案 3 :(得分:0)

尝试替换它:

convertView = inflater.inflate(R.layout.listitemview, null);  

其中listitemviewxml,其中定义了您的ImageViewTextView ...  希望这有助于你

答案 4 :(得分:0)

更改行

mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);

低于 * 在调试期间返回NULL * 通过

mHolder.textViewCenter = (TextView) convertView.findViewById(R.id.textview_list_item_central);