【发布时间】:2022-01-26 03:51:09
【问题描述】:
我正在使用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
【问题讨论】:
-
很高兴您解决了您的问题。请您考虑编辑您的初始问题以删除结果不重要的细节吗?谢谢
标签: java android kotlin exception search