0

我正在学习Android,并且已经完成了自定义控件。当我把它放在一个布局中时,它工作正常。我在 itemduplicados.xml 中是这样放的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.android.customViews.CtlDuplicado
        android:id="@+id/dupCancion"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

然后我想做一个很多这样的控件的列表,所以我在listadoduplicado.xml中使用了一个listview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LstListadoDuplicados"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>
</LinearLayout>

一个带有 onCreate 的 Activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listadoduplicados);
        AdaptDuplicados adapter = new AdaptDuplicados(this, Deduplicator.deduplicate());
        final ListView lstDuplicados = (ListView) findViewById(R.id.LstListadoDuplicados);
        lstDuplicados.setAdapter(adapter);
        lstDuplicados.setVisibility(View.VISIBLE);

    }

然后是一个扩展 arrayadapter 以使用 ctlduplicados 填充列表视图的类:

public AdaptDuplicados(Activity context, ArrayList<Duplicado> datos) {
    super(context, R.layout.itemduplicado, datos);
        this.context = context;
        this.datos = datos;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
                View item = convertView;
                DuplicadoViewHolder dvh;
                if (item == null){
                    LayoutInflater li = context.getLayoutInflater();
                    item = li.inflate(R.layout.itemduplicado, null);
                    dvh = new DuplicadoViewHolder();
                    dvh.duplicado = (CtlDuplicado)item.findViewById(R.id.dupCancion);
                    item.setTag(item);
                }else{
                    dvh = (DuplicadoViewHolder)item.getTag();
                }
                dvh.duplicado.setCanciones(datos.get(position));//here is the nullpointerexception cause duplicado is null
                return (item);
            }

好吧,我有一个空指针异常,因为 dvh.duplicato 是空的。

在 Eclipse 中调试我在行中看到了

item = li.inflate(R.layout.itemduplicado, null); 

该项目已创建,并且项目包含在名为 mChildren 的内部数组中良好创建的 CtlDuplicado,但是当我得到 item.findViewById(R.id.dupCancion) 时,它返回 null ...

R.id.dupCancion 在项目的 R 中定义。

有人可以告诉我发生了什么吗?

编辑:为空的 dvh.duplicato 是“if”中的那个,这个;

 dvh.duplicado = (CtlDuplicado)item.findViewById(R.id.dupCancion);

R.id.dupCancion 存在,并且在项目的内部数组中创建了一个名为 mChildren 的 CtlDuplicado,但是当我在项目中调用 findViewById 时,它返回 null ...

实现:

我尝试访问自定义控件中的控件,并且可以通过以下方式访问它们:

lbltxt1 = (TextView)item.findViewById(R.id.lbltxt1);
lbltxt2 = (TextView)item.findViewById(R.id.lbltxt2);
lblSubtxt1 = (TextView)item.findViewById(R.id.lblSubtxt1);

项目来自“如果”...

所以我可以访问我的控件的组件,但不能访问控件本身......

CtlDuplicado 已创建(我已通过调试器访问构造函数)其绘制正确但随后我无法通过 findViewById 访问它...但我可以通过该函数访问它们的组件... Ufff ...

4

1 回答 1

1

您没有设置实际视图(可能是错字)

item.setTag(item);

应该

item.setTag(dvh);
于 2012-06-27T01:33:47.710 回答