材质的android芯片在xml布局中膨胀时崩溃

时间:2018-11-11 09:02:15

标签: android material androidx material-components material-components-android

即使我不执行任何代码...看起来也好像在视图类drawable的某个地方创建了它,并且由于不允许这样做而使Chip崩溃...欢迎任何帮助。

android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class com.google.android.material.chip.Chip
    Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class com.google.android.material.chip.Chip
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at renetik.android.viewbase.CSView.inflate(CSView.java:104)
        at renetik.android.viewbase.CSView.getView(CSView.java:119)
        at renetik.android.viewbase.CSViewController.getView(CSViewController.java:405)
        at renetik.android.viewbase.CSViewController.getView(CSViewController.java:408)
        at renetik.android.viewbase.CSView.findView(CSView.java:108)
        at renetik.android.extensions.view.CSViewKt.editText(CSView.kt:22)
        at com.tanya.renetik.redriver.view.route.EditHouseFieldsController.<init>(EditHouseFieldsController.kt:15)
        at com.tanya.renetik.redriver.view.route.started.StartedAddHouseController.<init>(StartedAddHouseController.kt:31)
        at com.tanya.renetik.redriver.view.route.started.StartedRouteMapController$onCreate$2.invoke(StartedRouteMapController.kt:37)
        at com.tanya.renetik.redriver.view.route.started.StartedRouteMapController$onCreate$2.invoke(StartedRouteMapController.kt:17)
        at renetik.android.extensions.view.ViewKt$onClick$$inlined$apply$lambda$1.onClick(View.kt:51)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24697)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.UnsupportedOperationException: Do not set the background; Chip manages its own background drawable.
        at com.google.android.material.chip.Chip.setBackground(Chip.java:397)
        at android.view.View.<init>(View.java:5200)
        at android.widget.TextView.<init>(TextView.java:824)
        at android.widget.Button.<init>(Button.java:172)
        at android.widget.CompoundButton.<init>(CompoundButton.java:94)
        at android.widget.CheckBox.<init>(CheckBox.java:69)
        at android.widget.CheckBox.<init>(CheckBox.java:65)
        at androidx.appcompat.widget.AppCompatCheckBox.<init>(AppCompatCheckBox.java:64)
        at com.google.android.material.chip.Chip.<init>(Chip.java:169)

在布局中就像这样...

  <com.google.android.material.chip.Chip
            android:id="@+id/EditHouse_Category_Vacant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Category_Vacant" />

我的主题文件很简单:

    <style name="DriverTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/driver_primary</item>
        <item name="colorPrimaryDark">@color/driver_primary_dark</item>
        <item name="colorSecondary">@color/driver_secondary</item>
    </style>

</resources>

ext.kotlin_version ='1.3.0' ext.material_version ='1.0.0' ext.appcompat_version ='1.0.0' ext.play_version = '16 .0.0'

3 个答案:

答案 0 :(得分:1)

经过一天的战斗,这只是我的小解决方法:/

class ChipCrashWorkaround(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
    : Chip(context, attrs, defStyleAttr) {

    constructor (context: Context, attrs: AttributeSet?)
            : this(context, attrs, R.attr.chipStyle)

    constructor (context: Context) : this(context, null)

    override fun setBackground(background: Drawable) = try {
        super.setBackground(background)
    } catch (ex: UnsupportedOperationException) {
        warn(ex)
    }

    override fun setBackgroundDrawable(background: Drawable) = try {
        super.setBackgroundDrawable(background)
    } catch (ex: UnsupportedOperationException) {
        warn(ex)
    }
}

我可以清楚地看到,即使我在所有地方都禁用了所有自定义设置,也可能以某种方式将drawable从主题应用到了我的芯片上。.wtf我不知道,但必须继续前进...

答案 1 :(得分:0)

您正在使用哪个版本的库?因为芯片管理自己的背景并且不支持View#setBackground,所以引发了异常。您的芯片实例的父级是否有可能试图设置芯片的背景?

答案 2 :(得分:0)

好吧,我找到了,我正在使用以下代码创建顶层布局:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: blue
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: blue
    spec:
      containers:
      - name: blue-website
        image: scrapinghub/httpbin:latest
        command:
        - sleep
        - "3600"
        resources:
          requests:
            cpu: 0.1
            memory: 200

在此布局中,我设置了白色背景应该可以,但是... 现在,我看到了ContextThemeWrapper的实际作用:

view = FrameLayout(ContextThemeWrapper(this.context(), R.style.CSNavigationContainer))

因此,通过这种方式,我的上下文主题被修改为包含白色背景,并且以某种方式进入了后来使用该上下文创建的其他视图,并且由于不允许背景而使Chip崩溃了。

所以我不得不停止使用ContextThemeWrapper,也许我误解了实际目的,我想以编程方式将样式应用到我的视图中,什么是有效的,但它修改了整个主题。

所以我找到了paris库,用于以编程方式应用样式,并将代码更改为:

The specified theme will be applied on top of the base context's theme.

现在它可以使用相同的样式。