【发布时间】:2021-04-02 20:33:57
【问题描述】:
在我解释我的问题之前,我想说我已经看过所有相关的问题,但没有一个对我有用。
我是 Android 的初学者。我正在使用自定义视图类,假设 CustomView 看起来像:
class CustomView(context: Context?,attributeSet: AttributeSet) : View(context) {
class CustomView constructor(context: Context?, attributeSet: AttributeSet){
}
var number = 1
}
我在片段资源中扩充了这个视图,例如:
<com.example.myapp.CustomView
android:id="@+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
但是现在每当我尝试获取自定义视图时,它都是空的。所有同级视图都可以正常工作。
val root = inflater.inflate(R.layout.fragment_main, container,false)
val customView: CustomView = root.findViewById(R.id.custom_view)
报错了
java.lang.IllegalStateException: findViewById(R.id.custom_view) must not be null
【问题讨论】:
-
你为什么要膨胀视图,为什么不直接在片段中使用CustomeView。
-
因为片段也有其他组件
标签: android xml kotlin android-custom-view illegalstateexception