安卓:自定义视图从attrs.xml对象的引用,始终为null [英] Android: Custom View with object reference from attrs.xml, always null

查看:326
本文介绍了安卓:自定义视图从attrs.xml对象的引用,始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立对象之间的关系层次。每个对象有相同类型的父母为自己,或

I'm trying to setup a relationship hierarchy between objects. Every object has a parent of the same type as itself, or null.

我有一个的main.xml 包含其中的一些:

I have a main.xml that contains some of these:

<com.morsetable.MorseKey
    android:id="@+id/bi"
    android:layout_weight="1"
    custom:code=".."
    custom:parentKey="@id/be"
    android:text="@string/i" />

包含其中的一个水库/价值/ attrs.xml

<declare-styleable name="MorseKey">
    <attr name="code" format="string"/>
    <attr name="parentKey" format="reference"/>
</declare-styleable>

和一个类(这不是我的活动),其中包含这样的:

and a class (that is not my activity) that contains this:

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 实例报告ñ== 2 (好)和家长== NULL (坏)。更多的,家长== NULL 甚至当我明确地尝试将其设置到一些任意值(见注释)。我也曾尝试自定义:parentKey =@ + ID /是(带加号),但也不能工作。我在做什么错了?

This isn't working. Every MorseKey instance reports N == 2 (good) and parent == null (bad). More, parent == null even when I explicitly try setting it to some arbitrary value (see comment). I have also tried custom:parentKey="@+id/be" (with the plus sign) but that didn't work either. What am I doing wrong?

推荐答案

如果您MorseKey类是在一个单独的Java文件,我以为是从你说明书的情况下一类的(这不是我的活动)。然后,我相信这个问题是在你使用findViewById的()。 findViewById()将寻找MorseKey视图本身,而不是main.xml中的文件内的资源。

If your MorseKey class is in a separate java file, which I assume is the case from your statment "a class (that is not my activity)". Then I believe the problem is in your use of findViewById(). findViewById() will look for a resource within the MorseKey view itself rather than the main.xml file.

也许尝试获得MorseKey实例的父,并呼吁parent.findViewById()。

Maybe try getting the parent of the MorseKey instance and calling parent.findViewById().

case R.styleable.MorseKey_parentKey:
    parent = this.getParent().findViewById(a.getResourceId(attr, -1));

虽然这个如果你的MorseKey家长和孩子都在相同的布局才有效。

Though this will only work if your MorseKey parent and child are in the same layout.

<LinearLayout ...>
     <MorseKey ..../><!-- parent -->
     <MorseKey ..../><!-- child -->
</LinearLayout>

不过,这将是很难找到的看法,如果你的布局是这样的,在不同的布局,家长和孩子。

But it would be quite difficult to find the view if your layout is something like this with the parent and child in separate layouts.

<LinearLayout ...>
     <MorseKey ..../><!-- parent -->
</LinearLayout>
<LinearLayout ...>
     <MorseKey ..../><!-- child -->
</LinearLayout>

这篇关于安卓:自定义视图从attrs.xml对象的引用,始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆