Chip Drawable 在 Material UI 3 Android Kotlin 中给出错误

发布于 2025-01-14 04:34:23 字数 3214 浏览 0 评论 0原文

嘿,我正在使用 Material ui 3 的 Android 中工作。当我使用此代码时,我收到错误。我试图通过编程方式设置样式,但出现错误。我想通过使用谷歌提供的android芯片创建标签。但我不明白为什么我在运行活动时遇到错误。

implementation 'com.google.android.material:material:1.5.0'

TagHelper.kt

import android.content.Context
import android.view.ViewGroup
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipDrawable

object TagHelper {

    fun addTag(productTags: List<String>, viewGroup: ViewGroup) {
        productTags.forEachIndexed { _, tagString ->
            val chip = Chip(viewGroup.context)
            chip.isClickable = false
            setupChipAppearance(viewGroup.context, chip)
            chip.text = tagString
            viewGroup.addView(chip)
        }
    }

    private fun setupChipAppearance(
        context: Context,
        chip: Chip
    ) {
        val drawable =
            ChipDrawable.createFromAttributes(context, null, 0, R.style.ProductChip)
        chip.setChipDrawable(drawable)
    }
}

style.xml

 <style name="ProductChip" parent="@style/Widget.Material3.Chip.Input">
   // style attributes
 </style>

错误

2022-03-15 12:42:32.430 18818-18818/com.example.app.dev E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app.dev, PID: 18818
    java.lang.NullPointerException: Attempt to invoke virtual method 'float com.google.android.material.resources.TextAppearance.getTextSize()' on a null object reference
        at com.google.android.material.chip.ChipDrawable.loadFromAttributes(ChipDrawable.java:359)
        at com.google.android.material.chip.ChipDrawable.createFromAttributes(ChipDrawable.java:280)
        at com.example.app.TagHelper.setupChipAppearance(TagHelper.kt:26)
        at com.example.app.TagHelper.addProductTags(TagHelper.kt:15)
        at com.example.app.products.ProductActivity.setupSaveView(ProductActivity.kt:410)
        at com.example.app.products.ProductActivity.setupViewModel$lambda-4(ProductActivity.kt:93)
        at com.example.app.products.ProductActivity.lambda$XbNDghVZtofDBOgHXRVZECmW9aw(Unknown Source:0)
        at com.example.app.products.-$$Lambda$ProductActivity$XbNDghVZtofDBOgHXRVZECmW9aw.onChanged(Unknown Source:4)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.lifecycle.LiveData$1.run(LiveData.java:93)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

hey I am working in android with material ui 3. I am getting error when I used this code. I am trying to set style through programmatically and I am getting error. I want to create tag through using android chip provide by google. But I don't understand why I am getting error my running the activity.

implementation 'com.google.android.material:material:1.5.0'

TagHelper.kt

import android.content.Context
import android.view.ViewGroup
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipDrawable

object TagHelper {

    fun addTag(productTags: List<String>, viewGroup: ViewGroup) {
        productTags.forEachIndexed { _, tagString ->
            val chip = Chip(viewGroup.context)
            chip.isClickable = false
            setupChipAppearance(viewGroup.context, chip)
            chip.text = tagString
            viewGroup.addView(chip)
        }
    }

    private fun setupChipAppearance(
        context: Context,
        chip: Chip
    ) {
        val drawable =
            ChipDrawable.createFromAttributes(context, null, 0, R.style.ProductChip)
        chip.setChipDrawable(drawable)
    }
}

style.xml

 <style name="ProductChip" parent="@style/Widget.Material3.Chip.Input">
   // style attributes
 </style>

Error

2022-03-15 12:42:32.430 18818-18818/com.example.app.dev E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app.dev, PID: 18818
    java.lang.NullPointerException: Attempt to invoke virtual method 'float com.google.android.material.resources.TextAppearance.getTextSize()' on a null object reference
        at com.google.android.material.chip.ChipDrawable.loadFromAttributes(ChipDrawable.java:359)
        at com.google.android.material.chip.ChipDrawable.createFromAttributes(ChipDrawable.java:280)
        at com.example.app.TagHelper.setupChipAppearance(TagHelper.kt:26)
        at com.example.app.TagHelper.addProductTags(TagHelper.kt:15)
        at com.example.app.products.ProductActivity.setupSaveView(ProductActivity.kt:410)
        at com.example.app.products.ProductActivity.setupViewModel$lambda-4(ProductActivity.kt:93)
        at com.example.app.products.ProductActivity.lambda$XbNDghVZtofDBOgHXRVZECmW9aw(Unknown Source:0)
        at com.example.app.products.-$Lambda$ProductActivity$XbNDghVZtofDBOgHXRVZECmW9aw.onChanged(Unknown Source:4)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.lifecycle.LiveData$1.run(LiveData.java:93)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2025-01-21 04:34:23

我有类似的问题。但我通过降级解决了这个问题

com.google.android.material:material:1.4.0

I had a similar issue. But I solved this problem by downgrading to

com.google.android.material:material:1.4.0
独﹏钓一江月 2025-01-21 04:34:23

与今天有同样的问题,但我通过使用以下方法解决了它:

style="@style/Widget.MaterialComponents.Chip.Filter"

而不是:

style="@style/Widget.Material3.Chip.Filter"

Had the same problem as of today, but I resolved it by using:

style="@style/Widget.MaterialComponents.Chip.Filter"

Instead of:

style="@style/Widget.Material3.Chip.Filter"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文