资源ids.xml提供相同的ID Android

时间:2016-06-02 09:13:05

标签: android xml android-resources

我遇到了一个奇怪的问题,我在res / values / ids.xml文件中设置了自定义视图的ID:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="observation_photo_view" type="id"/>
        <item name="path_view" type="id"/>
    </resources>

这应该为这些字符串提供唯一的ID。

然后我以编程方式设置ID:

public ObservationPhotoView(Context context){
    super(context);
    this.context = context;
    setId(R.id.observation_photo_view); //setting it here
    setOrientation(VERTICAL);
}

我的自定义路径视图也是一样。

然而,当我试图通过

来获取我的ObverationPhotoView时
    (ObservationPhotoView) parentView.findViewById(R.id.observation_photo_view);

我收到错误:

    Caused by: java.lang.ClassCastException: CustomPathView cannot be cast to ObservationPhotoView

通过调试器检查这些视图后,我可以看到它们确实相同。 comparing the two values via debugger

对此有任何解释吗?

1 个答案:

答案 0 :(得分:0)

好吧,看起来像重建项目有效,抱歉打扰。

我希望如果有人遇到同样的问题,我们会提供帮助。