我试图建立对象之间的关系层次结构.每个对象都具有与其自身相同类型的父对象,或者为null.
我有一个main.xml,其中包含其中一些:
android:id="@+id/bi"
android:layout_weight="1"
custom:code=".."
custom:parentKey="@id/be"
android:text="@string/i" />
包含以下之一的res / values / attrs.xml:
和一个包含以下内容的类(不是我的活动):
public class MorseKey extends Button {
public MorseKey(Context context, AttributeSet attrs) {
super(context, attrs);
initMorseKey(attrs);
}
private void initMorseKey(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.MorseKey);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.MorseKey_code:
code = a.getString(attr);
break;
case R.styleable.MorseKey_parentKey:
parent = (MorseKey)findViewById(a.getResourceId(attr, -1));
//parent = (MorseKey)findViewById(R.id.be);
Log.d("parent, N:", ""+parent+","+N);
break;
}
}
a.recycle();
}
private MorseKey parent;
private String code;
}
这不起作用.每个MorseKey实例报告N == 2(好),而父== null(坏).更多,parent == null,即使我明确尝试将其设置为任意值(请参见注释).我也尝试过custom:parentKey =“ @ id / be”(带有加号),但也没有用.我究竟做错了什么?