Android:listview:自定义项:nullpointerexception,findviewbyid 返回null [英] Android: listview: custom items: nullpointerexception, findviewbyid returns null

查看:16
本文介绍了Android:listview:自定义项:nullpointerexception,findviewbyid 返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌搜索并搜索以解决此错误一段时间,但我似乎无法找出原因以及如何解决它.

I have been googling and searching to resolve this error for some time and I can`t seem to find out why and how to solve it.

我正在使用 customAdapter 来填充我的列表视图.我膨胀了描述我的 listItem 的 xml.我创建 viewHolder 对象并使用 findViewById 加载到我的文本视图中.之后我想设置该 textview 的文本,但它 findViewbyid 返回一个空值.所以自动解析为空指针异常.

I`m using a customAdapter to fill in my listview. I inflate the xml describing my listItem. I make viewHolder object and load in my textview using findViewById. After that i want to set the text of that textview but it findViewbyid returns a null value. So automaticcally resolving into a 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" />

感谢任何帮助!

推荐答案

在您膨胀的 convertView 中搜索视图(而不是在当前的 Activity 布局中,例如你目前这样做):

Search for the views in the convertView that you inflate(and not in the current Activity layout like you currently do):

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

这篇关于Android:listview:自定义项:nullpointerexception,findviewbyid 返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆