[英]android - kotlin, custom view, binding, inflating - JNI ERROR (app bug): local reference table overflow (max=512)
I am using kotlin.我正在使用 kotlin。 I am trying create Custom View inside Custom View.
我正在尝试在自定义视图中创建自定义视图。 I hope, I did it.
我希望,我做到了。
Now I would like to change text in my views.现在我想在我的视图中更改文本。 I tried use binding.
我尝试使用绑定。 But when is called: SampleMyViewBBinding.inflate(inflater) application shows me error:
但是当被调用时: SampleMyViewBBinding.inflate(inflater) 应用程序显示错误:
JNI ERROR (app bug): local reference table overflow (max=512) JNI ERROR (app bug): local reference table overflow (max=512)
I think application trying call inflating again, again, again, .... Can you help me, what is wrong?我认为应用程序试图再次调用充气,再次,再次,......你能帮我,有什么问题吗? How to use binding in Custom View?
如何在自定义视图中使用绑定? Thank you.
谢谢你。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
class MyViewA : ConstraintLayout {
constructor(context: Context) : super(context) { init(null, 0) }
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { init(attrs, 0) }
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { init(attrs, defStyle) }
private fun init(attrs: AttributeSet?, defStyle: Int) {
}
}
class MyViewB : ConstraintLayout {
constructor(context: Context) : super(context) { init(null, 0) }
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { init(attrs, 0) }
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { init(attrs, defStyle) }
private fun init(attrs: AttributeSet?, defStyle: Int) {
Log.i("AW", "init test...1")
var inflater = LayoutInflater.from(context)
Log.i("AW", "init test...2")
var binding = SampleMyViewBBinding.inflate(inflater)
Log.i("AW", "init test...3")
}
}
activity_main.xml: activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_my_view_MAIN" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_Main">
<include layout="@layout/sample_my_view_a" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
sample_my_view_a.xml: sample_my_view_a.xml:
<com.myapp.mytestingapplication.MyViewA
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_MyViewA">
<TextView
android:id="@+id/text_MyViewA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_my_view_A" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_MyViewA">
<include layout="@layout/sample_my_view_b" />
</LinearLayout>
</com.myapp.mytestingapplication.MyViewA>
sample_my_view_b.xml: sample_my_view_b.xml:
<com.myapp.mytestingapplication.MyViewB
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_MyViewB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_my_view_B" />
</com.myapp.mytestingapplication.MyViewB>
I think you messed with the constructors and the init block a bit.我认为你搞砸了构造函数和 init 块。 You can use
JvmOverloads
to "automatically" generate the constructors for you.您可以使用
JvmOverloads
为您“自动”生成构造函数。 Also, the init block as written below is invoked regardless the constructor being used:此外,无论使用的构造函数如何,都会调用下面编写的 init 块:
class MyViewA @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : ConstraintLayout(context, attrs, defStyle) {
init {
//
}
}
class MyViewB @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : ConstraintLayout(context, attrs, defStyle) {
init {
Log.i("AW", "init test...1")
var inflater = LayoutInflater.from(context)
Log.i("AW", "init test...2")
var binding = SampleMyViewBBinding.inflate(inflater)
Log.i("AW", "init test...3")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.