Chip Drawable 在 Material UI 3 Android Kotlin 中给出错误
嘿,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有类似的问题。但我通过降级解决了这个问题
I had a similar issue. But I solved this problem by downgrading to
与今天有同样的问题,但我通过使用以下方法解决了它:
而不是:
Had the same problem as of today, but I resolved it by using:
Instead of: