I want to add a Chip view to ChipGroup in my project but always I get below error
My code in Activity
for (int i = 0; i < names.size(); i++) {
Chip chip = new Chip(getApplicationContext());
chip.setText(names.get(i));
chip.setId(i);
chipGroup.addView(chip);
}
and Error is
java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:248)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:222)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:150)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:81)
at com.google.android.material.chip.ChipDrawable.loadFromAttributes(ChipDrawable.java:334)
at com.google.android.material.chip.ChipDrawable.createFromAttributes(ChipDrawable.java:276)
at com.google.android.material.chip.Chip.<init>(Chip.java:193)
at com.google.android.material.chip.Chip.<init>(Chip.java:186)
at com.google.android.material.chip.Chip.<init>(Chip.java:182)
My app theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
As shown in the code above my theme is right!
After a while I found that need to pass Activity to Chip instead pass context
The code is right below.