CustomListView里面的片段

时间:2014-04-09 11:47:05

标签: android android-fragments

需要在片段上显示自定义列表视图,但每次都会抛出空指针异常。

请在我出错的地方帮助我。

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {

            convertView = layoutinflater
                    .inflate(R.layout.custom_list_row, null);
            holder.Name = (TextView) convertView
                    .findViewById(R.id.tv_download_label);
            holder.Download = (Button) convertView
                    .findViewById(R.id.btn_download);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.Name.setText(mNames[position]);
        holder.Download.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView Name;
        Button Download;
    }

和on createview方法如下

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
           mView = inflater.inflate(R.layout.fragment_gurbani_list, container, false);
           mLvList=(ListView)mView.findViewById(R.id.lv_list);
           mTvDownload=(TextView)mView.findViewById(R.id.tv_download_label);
           switch (Utility.gurbaniItemSelected) {
           case 1:
            names=Utility.getInstance().getJaapJiSahib();
            break;
        }      

          mAdapter=new CustomListAdapter(getActivity(), names);
          mLvList.setAdapter(mAdapter);  
        return mView;
    }

custom_list_row.xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="@dimen/margin_10dp"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_custom_list"
        android:layout_width="wrap_content"

        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" />

    <Button
        android:id="@+id/btn_download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

你应该替换这个

 holder.Name = (TextView) convertView
                .findViewById(R.id.tv_download_label);

使用

 holder.Name = (TextView) convertView
                .findViewById(R.id.tv_custom_list);

becoz在您的虚增布局中没有TextView ID tv_download_label

答案 1 :(得分:0)

  

此行holder.Name.setText(mNames [position]);

如果您说这是NullPointerException,那么Name为空。检查custom_list_row.xml是否包含ID为tv_download_label的文本视图。我不认为你在扩充的布局中有textview。因此初始化失败。

custom_list_row.xml中,您只有一个ID为tv_custom_list

的文字视图
<TextView
    android:id="@+id/tv_custom_list" // id is tv_custom_list

所以改变这个

holder.Name = (TextView) convertView
                .findViewById(R.id.tv_download_label);

holder.Name = (TextView) convertView
                .findViewById(R.id.tv_custom_list);