Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a862f8ed authored by Edgar Wang's avatar Edgar Wang
Browse files

[SettingsLib] SegmentedButtonPreference fix NullPointerException when view is inflating

The ButtonGroup can not be empty, it will get NPE if all children's
visibility are GONE when inflating. So we should set all button's
visibility GONE and ButtonGroup visibility to GONE in xml, and turn on
ButtonGroup when the data is not empty.

Bug: 414319180
Test: manual
Flag: EXEMPT bug fix
Change-Id: Ib9c8cb2b7ddec8c22418ec6e0890cb9cf6280292
parent ab2d0a91
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -30,30 +30,35 @@
        style="@style/Widget.Material3Expressive.MaterialButtonGroup.Connected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:singleSelection="true"
        app:selectionRequired="true"
        app:checkedButton="@id/button_1">
        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_1"
            android:layout_weight="1"
            android:visibility="gone"
            app:iconPadding="0dp"
            app:iconGravity="textStart"
            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_2"
            android:layout_weight="1"
            android:visibility="gone"
            app:iconPadding="0dp"
            app:iconGravity="textStart"
            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_3"
            android:layout_weight="1"
            android:visibility="gone"
            app:iconPadding="0dp"
            app:iconGravity="textStart"
            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_4"
            android:layout_weight="1"
            android:visibility="gone"
            app:iconPadding="0dp"
            app:iconGravity="textStart"
            style="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"/>
+5 −0
Original line number Diff line number Diff line
@@ -117,6 +117,11 @@ class SegmentedButtonPreference @JvmOverloads constructor(
    }

    private fun applyButtonSetupData() {
        // The button group is default gone to avoid NullPointerException
        // if all children's visibility are GONE.
        if(buttonSetupData.isNotEmpty()) {
            buttonGroup?.isGone = false
        }
        for ((index, config) in buttonSetupData) {
            applyButtonSetupData(index, config.first, config.second)
        }