オブジェクト間の関係階層を設定しようとしています。すべてのオブジェクトには、それ自体と同じタイプの親、または。がありnull
ます。
私はmain.xml
これらのいくつかを含むを持っています:
<com.morsetable.MorseKey
android:id="@+id/bi"
android:layout_weight="1"
custom:code=".."
custom:parentKey="@id/be"
android:text="@string/i" />
これらのいずれかを含むa res/values/attrs.xml
:
<declare-styleable name="MorseKey">
<attr name="code" format="string"/>
<attr name="parentKey" format="reference"/>
</declare-styleable>
そしてこれを含むクラス(私の活動ではありません):
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
parent == null
parent == null
custom:parentKey="@+id/be"