繁体   English   中英

android - kotlin,自定义视图,绑定,膨胀 - JNI 错误(应用程序错误):本地引用表溢出(最大值 = 512)

[英]android - kotlin, custom view, binding, inflating - JNI ERROR (app bug): local reference table overflow (max=512)

我正在使用 kotlin。 我正在尝试在自定义视图中创建自定义视图。 我希望,我做到了。

现在我想在我的视图中更改文本。 我尝试使用绑定。 但是当被调用时: SampleMyViewBBinding.inflate(inflater) 应用程序显示错误:

JNI ERROR (app bug): local reference table overflow (max=512)

我认为应用程序试图再次调用充气,再次,再次,......你能帮我,有什么问题吗? 如何在自定义视图中使用绑定? 谢谢你。

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:

<?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:

<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:

<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>

我认为你搞砸了构造函数和 init 块。 您可以使用JvmOverloads为您“自动”生成构造函数。 此外,无论使用的构造函数如何,都会调用下面编写的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM