[英]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.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>
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.尝试使用 android:iconifiedByDefault="false" 而不是 app:iconifiedByDefault。 If it does not work, share your java or Kotlin code please
如果它不起作用,请分享您的 java 或 Kotlin 代码
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所以我修复了它,问题是我在搜索视图中使用了主题样式并且样式是从 EditTextView 继承的我将其更改为 SearchView 并且问题已修复
Before - After -之前 - 之后 -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.