简体   繁体   中英

How avoid java.lang.NullPointerException

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

Try to use android:iconifiedByDefault="false" not app:iconifiedByDefault. If it does not work, share your java or Kotlin code please

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 - After -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM