【问题标题】:android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating classandroid.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class
【发布时间】:2017-03-27 09:41:04
【问题描述】:

我目前在我的应用程序中遇到问题,我已经查看并尝试了许多解决方案,但我找不到任何真正有效的解决方案。

我有一个包含自定义组件列表的活动。这些组件是包装在 LinearLayout 中的 android CardView 元素。

每个卡片组件都包含一个默认为空的相对布局。

在activity XML文件中调用组件时,我添加了自定义属性app:partLayout="@layout/choice_access_part"。这个属性在我的组件代码中处理。之后,我膨胀在 partLayout 属性中引用的布局添加我尝试将它添加到我的组件中的 RelativeLayout。

val inflatedLayout: View = inflater.inflate(partLayout, selectionContainer, false)
selectionContainer.addView(inflatedLayout)

我编译应用程序并运行它没有问题,直到我到达这部分代码并得到错误:android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class

组件代码

class MultipleChoiceCard(context: Context?, attrs: AttributeSet?) : CardView(context, attrs) {

    val title: TextView
    val nonMandatoryText: TextView
    val selectionContainer: RelativeLayout

    init {
        val inflater: LayoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.component_multiple_choice, this)

        // Getting elements in XML
        title = find(R.id.multiple_choice_title)
        nonMandatoryText = find(R.id.non_mandatory_text)
        selectionContainer = find(R.id.selection_container)

        // Extract value from XML
        val a = context.theme.obtainStyledAttributes(attrs, R.styleable.MultipleChoiceCard, 0, 0)
        val titleValue = a.getString(R.styleable.MultipleChoiceCard_title)
        val nonMandatoryTextValue = a.getString(R.styleable.MultipleChoiceCard_nonMandatoryText) ?: ""
        val colorValue = a.getInt(R.styleable.MultipleChoiceCard_cardColor, R.color.user_deep_orange)
        val partLayout = a.getInt(R.styleable.MultipleChoiceCard_partLayout, R.layout.choice_rythm_part)

        // Applying the XML values to the wanted elements
        title.text = titleValue
        title.setTextColor(colorValue)
        // Set the non mandatory text visible if the value is not an empty string
        if(nonMandatoryTextValue != ""){
            nonMandatoryText.visibility = View.VISIBLE
            nonMandatoryText.text = nonMandatoryTextValue
        }

        val inflatedLayout: View = inflater.inflate(partLayout, selectionContainer, false)
        selectionContainer.addView(inflatedLayout)

        a?.recycle()

    }

}

组件 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="20dp"
        android:id="@+id/rythm_card"
        app:cardBackgroundColor="#2d292a">

        <RelativeLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:padding="20dp">

            <TextView
                android:id="@+id/multiple_choice_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="true"
                android:textColor="@color/user_deep_orange"
                android:layout_marginBottom="10dp"
                android:textStyle="bold">
            </TextView>

            <TextView
                android:id="@+id/non_mandatory_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/multiple_choice_title"
                android:textColor="#8c8f91"
                android:textSize="13sp"
                android:textStyle="italic"
                android:visibility="gone"
                android:layout_marginBottom="10dp">
            </TextView>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/non_mandatory_text"
                android:id="@+id/selection_container">

            </RelativeLayout>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

布局作为属性传递

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/non_mandatory_text"
        android:id="@+id/rythm_radio_group">

        <RadioButton
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Libre"
            android:textSize="20sp"
            android:layoutDirection="rtl"
            android:buttonTint="@color/user_deep_orange"
            android:layout_marginBottom="10dp">

        </RadioButton>

        <RadioButton
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Toutes les 10 secondes"
            android:textSize="20sp"
            android:layoutDirection="rtl"
            android:buttonTint="@color/user_deep_orange"
            android:layout_marginBottom="10dp">

        </RadioButton>

        <RadioButton
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Toutes les 30 secondes"
            android:textSize="20sp"
            android:layoutDirection="rtl"
            android:buttonTint="@color/user_deep_orange"
            android:layout_marginBottom="10dp">

        </RadioButton>

        <RadioButton
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Toutes les minutes"
            android:textSize="20sp"
            android:layoutDirection="rtl"
            android:buttonTint="@color/user_deep_orange">

        </RadioButton>

    </RadioGroup>


</LinearLayout>

【问题讨论】:

  • 放这个类的xml
  • 你能分享 .xml 文件吗?特别是第 32 行

标签: android android-layout android-studio kotlin


【解决方案1】:

经过搜索,我找到了解决方案,这只是一个愚蠢的错误。

val partLayout = a.getInt(R.styleable.MultipleChoiceCard_partLayout, R.layout.choice_rythm_part) 行必须得到一个 resourceId 而不是一个 int,所以解决方案是将 getInt 替换为 getResourceId

代码应该是这样的。

val partLayout = a.getResourceId(R.styleable.MultipleChoiceCard_partLayout, R.layout.choice_rythm_part)

我在post 上找到了解决方案。

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 2016-11-03
    • 2017-06-05
    • 2017-01-13
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多