SearchView因通货膨胀错误(androidx)而崩溃-资源未找到异常

时间:2019-03-06 07:59:27

标签: android searchview androidx

我的布局之一为SearchView,并且在 some 设备上出现了崩溃。问题是找不到abc_textfield_search_material资源。但是我没有直接使用此资源,因为我的xml布局看起来像这样:

<androidx.appcompat.widget.SearchView
        android:id="@+id/svSearch"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
        android:queryHint="@string/filter"
        app:iconifiedByDefault="false"
        android:paddingBottom="4dp"
        app:queryHint="@string/filter" />

任何想法如何解决这个问题?仅当稀有设备(少于1%)发生此崩溃时,大多数设备才能正常工作。我已经在考虑使用复合可绘制对象将其更改为TextView了,但是也许有比避免使用SearchView

更好的解决方案。

Stacktrace

java.lang.RuntimeException: Unable to pause activity {com.my.app/com.my.app.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class androidx.appcompat.widget.SearchView
    at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4071)
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4026)
    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3978)
    at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1818)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6744)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class androidx.appcompat.widget.SearchView
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class androidx.appcompat.widget.SearchView
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:647)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    ...
Caused by: android.content.res.Resources$NotFoundException: Drawable com.my.app:drawable/abc_textfield_search_material with resource ID #0x7f080058
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_textfield_search_material.xml from drawable resource ID #0x7f080058
    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:847)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:631)
    at android.content.res.Resources.getDrawableForDensity(Resources.java:888)
    at android.content.res.Resources.getDrawable(Resources.java:827)
    at android.content.Context.getDrawable(Context.java:635)
    at androidx.core.content.ContextCompat.a(ContextCompat.java:463)
    at androidx.appcompat.widget.AppCompatDrawableManager.a(AppCompatDrawableManager.java:203)
    at androidx.appcompat.widget.AppCompatDrawableManager.a(AppCompatDrawableManager.java:191)
    at androidx.appcompat.content.res.AppCompatResources.b(AppCompatResources.java:102)
    at androidx.appcompat.widget.TintTypedArray.a(TintTypedArray.java:76)
    at androidx.appcompat.widget.SearchView.<init>(SearchView.java:298)
    at androidx.appcompat.widget.SearchView.<init>(SearchView.java:270)
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:647)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    ...

主题

我在我的应用中使用了android x主题-Theme.MaterialComponents.NoActionBar.Bridge

2 个答案:

答案 0 :(得分:0)

它抱怨:

Caused by: android.content.res.Resources$NotFoundException:
Drawable com.my.app:drawable/abc_textfield_search_material with resource ID #0x7f080058

Caused by: android.content.res.Resources$NotFoundException:
File res/drawable/abc_textfield_search_material.xml from drawable resource ID #0x7f080058

应将{em> 包含在androidx.appcompat 1.0.2库本身中。

将该资源直接添加到应用程序的资源中可能会有所帮助;

@drawable/abc_textfield_search_activated_mtrl_alpha

@drawable/abc_textfield_search_default_mtrl_alpha

Resources$NotFoundException可能有多种原因……

但是“个人命运” <1%不能告诉哪个设备或哪个API级别。

a)view & restrict your app's compatible devices可以轻松地摆脱它们。

b)提交issue可能是另一种选择;如果可能的话,请提供上述设备的详细信息。

答案 1 :(得分:0)

现在,我最终使用了类似这样的东西,而不是SearchView

https://github.com/droidparts/droidparts/blob/master/droidparts-misc/src/org/droidparts/widget/ClearableEditText.java

带有EditText的简单CompoundDrawable和触摸侦听器以显示此清晰的图标...

不过,如果有人找到解决问题的方法,请告诉我。