簡體   English   中英

如何避免 java.lang.NullPointerException

[英]How avoid java.lang.NullPointerException

我正在使用androidx.appcompat.widget.SearchView ,正如我在我的 searchView 應用程序中設置此app:iconifiedByDefault="false"屬性拋出java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Drawable.getIntrinsicWidth()' on a null object reference有沒有辦法避免拋出這個異常?

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

例外

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Drawable.getIntrinsicWidth()' on a null object reference

嘗試使用 android:iconifiedByDefault="false" 而不是 app:iconifiedByDefault。 如果它不起作用,請分享您的 java 或 Kotlin 代碼

所以我修復了它,問題是我在搜索視圖中使用了主題樣式並且樣式是從 EditTextView 繼承的我將其更改為 SearchView 並且問題已修復

之前 - 之后 -

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM