Android:listview:CustomItems:nullpointerexception,findviewbyid返回null

Android:listview:CustomItems:nullpointerexception,findviewbyid返回null,android,listview,android-listview,nullpointerexception,adapter,Android,Listview,Android Listview,Nullpointerexception,Adapter,一段时间以来,我一直在谷歌上搜索并试图解决这个错误,但我似乎无法找出原因和解决方法 我正在使用customAdapter填充我的listview。我膨胀了描述我的listItem的xml。我使用findViewById创建viewHolder对象并加载到我的文本视图中。之后,我想设置textview的文本,但它findViewbyid返回空值。所以自动解析为nullpointerexception 适配器: /** CLASS : Custom Adapter for the listview

一段时间以来,我一直在谷歌上搜索并试图解决这个错误,但我似乎无法找出原因和解决方法

我正在使用customAdapter填充我的listview。我膨胀了描述我的listItem的xml。我使用findViewById创建viewHolder对象并加载到我的文本视图中。之后,我想设置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;
    }
}
/**类:listview的自定义适配器*/
私有类CustomAdapter扩展了ArrayAdapter{
私人清单项目;
私有上下文;
私人内部布局资源;
私人持票人;
公共CustomAdapter(上下文上下文、int-layoutId、列表对象){
超级(上下文、布局ID、对象);
layoutResourceId=layoutId;
项目=对象;
mContext=上下文;
}       
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(layoutResourceId,父项,false);
mHolder=新的ViewHolder();
******调试期间返回NULL******
mHolder.textViewCenter=(TextView)findViewById(R.id.TextView\u list\u item\u central);
convertView.setTag(mHolder);
}否则{
mHolder=(ViewHolder)convertView.getTag();
}
跟踪=项目。获取(位置);
******这里发生了错误******
mHolder.textViewCenter.setText(track.getTrack());
返回视图;
}
类视图持有者{
图像视图图像视图;
文本视图文本视图中心;
TextView textViewRight;
}
}
XML:

?xml version=“1.0”encoding=“utf-8”>


感谢您的帮助

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


您正在膨胀布局,但在获取其视图时未使用它

这样做:

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

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();
         }
尝试替换此:

convertView = inflater.inflate(R.layout.listitemview, null);  
其中
listitemview
是您的
xml
,其中您定义了
ImageView
TextView
s..
希望这能帮助你改变路线

mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);
低于*的在调试过程中返回空值*

convertView = inflater.inflate(R.layout.listitemview, null);  
mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);
mHolder.textViewCenter = (TextView) convertView.findViewById(R.id.textview_list_item_central);