首先:是的,我阅读了关于这个主题的所有其他主题.不仅是来自这个网站的人......(你看,我有点沮丧)
他们中的大多数都提供了使用建议,android:id而不仅仅是id在XML文件中.我做到了.
我从其他人那里了解到,View.findViewById与众不同Activity.findViewById.我也是这样处理的.
在我location_layout.xml,我使用:
<FrameLayout .... >
<some.package.MyCustomView ... />
<LinearLayout ... >
<TextView ...
android:id="@+id/txtLat" />
...
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在我的活动中,我做了:
...
setContentView( R.layout.location_layout );
Run Code Online (Sandbox Code Playgroud)
在我的自定义视图类中:
...
TextView tv = (TextView) findViewById( R.id.txtLat );
Run Code Online (Sandbox Code Playgroud)
返回null.这样做,我的活动工作正常.也许这是因为Activity.findViewById和View.findViewById差异.所以我在本地存储了传递给海关视图构造函数的上下文并尝试:
...
TextView tv = (TextView) ((Activity) context).findViewById( R.id.txtLat );
Run Code Online (Sandbox Code Playgroud)
这也回来了null.
然后,我改变了我的自定义视图扩展ViewGroup,而不是View和改变location_layout.xml让TextView我的自定义视图的直接子,从而使View.findViewById应该工作的设想.惊喜:它没有解决任何问题.
那么我做错了什么?
我会感激任何评论.
如何在Kotlin中声明辅助构造函数?
有没有关于这方面的文件?
以下不编译......
class C(a : Int) {
// Secondary constructor
this(s : String) : this(s.length) { ... }
}
Run Code Online (Sandbox Code Playgroud)