0

I'm using android.support.v7.widget.SearchView like the following code.

    <android.support.v7.widget.SearchView 
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@id/img_search"
        android:closeIcon="@null"
        android:searchIcon="@null"
        android:iconifiedByDefault="false"
        android:queryBackground="@android:color/transparent"/>

NPE occurred only in api level 21 with the fatal log.

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.setState(int[])' on a null object reference
        at android.widget.SearchView.updateCloseButton(SearchView.java:849)
        at android.widget.SearchView.updateViewsVisibility(SearchView.java:798)

I figure it happened 'cause I set attribute is null of closeIcon and searchIcon.

How can I solve this problem wisely?

libliboom
  • 532
  • 5
  • 17

1 Answers1

0

I solved it with the following code.

    <android.support.v7.widget.SearchView 
        android:id="@+id/search_view"
        style="@style/LanguageSearchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@id/img_search"
        android:iconifiedByDefault="false"
        android:queryBackground="@android:color/transparent"/>

Actually I made style for api level 21 in value-v21/styles.xml.

    <style name="LanguageSearchView">
        <item name="closeIcon">@null</item>
        <item name="searchIcon">@null</item>
    </style>

All api level except for 21, they will use the following style in value/styles.xml.

    <style name="LanguageSearchView">
        <item name="android:closeIcon">@null</item>
        <item name="android:searchIcon">@null</item>
    </style>

I just know with prefix of android is framework attribute,
otherwise it's own application attribute.

But I'm still confusing about this solution.
Anyone who can explain it clearly?

libliboom
  • 532
  • 5
  • 17
  • Got the same crash here. Do you have any reference to your solution? And does it really work? Thanks. – iForests Aug 13 '21 at 16:43