Reputation: 354
I'm using androidx.appcompat.widget.SearchView
as I noticed when I set this app:iconifiedByDefault="false"
property in my searchView app throws java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Drawable.getIntrinsicWidth()' on a null object reference
is there a way to avoid throwing this exception?
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context=".navigation.home.search.GameSearchFragment">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/searchToolbar"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp">
<androidx.appcompat.widget.SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/primaryColor"
app:defaultQueryHint="Search Games"
app:iconifiedByDefault="false"
android:theme="@style/SearchStyle"
app:searchIcon="@null"
app:queryBackground="@android:color/transparent">
</androidx.appcompat.widget.SearchView>
</com.google.android.material.appbar.MaterialToolbar>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/searchViewBorder"
app:layout_constraintEnd_toEndOf="@+id/searchToolbar"
app:layout_constraintStart_toStartOf="@+id/searchToolbar"
app:layout_constraintTop_toBottomOf="@+id/searchToolbar">
</View>
</androidx.constraintlayout.widget.ConstraintLayout>
Exception
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Drawable.getIntrinsicWidth()' on a null object reference
Upvotes: 0
Views: 308
Reputation: 728
I ran into the same issue and it was because I was using an EditText but the style was inheriting from TextView, weirdly the EditText worked until the user entered some text in it and then when the field received focus it would crash with java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Drawable.getIntrinsicWidth()' on a null object reference
.
Hope this helps someone...
Before (caused crash)
<style name="InputMethodEditText" parent="@android:style/Widget.TextView">
After (no crash)
<style name="InputMethodEditText" parent="@android:style/Widget.EditText">
Upvotes: 0
Reputation: 354
So I fixed it, The problem was that I used theme style in search View and style was inherited from EditTextView I changed it to SearchView And problem was fixed
Before - <style name="SearchStyle" parent="Widget.AppCompat.EditText">
After - <style name="SearchStyle" parent="Widget.AppCompat.SearchView">
Upvotes: 1
Reputation: 90
Try to use android:iconifiedByDefault="false" not app:iconifiedByDefault. If it does not work, share your java or Kotlin code please
Upvotes: 0