我目前在我的應用程序中遇到了一個問題,我查看並嘗試了很多解決方案,但無法使用找到任何真正有用的東西。android.view.InflateException:二進制XML文件行#32:二進制XML文件行#32:錯誤充氣類
我有一個包含自定義組件列表的活動。這些組件是包裝在LinearLayout中的android CardView元素。
這些卡組件中的每一個都包含默認情況下爲空的相對佈局。
在活動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線 –