搜索视图在充气时崩溃(androidx)-资源未找到异常。

24

我在我的布局中有一个SearchView,在某些设备上会出现附加的崩溃。问题是找不到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 更好的解决方案。

堆栈跟踪

<code><strong><code>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)
    ...
</code></strong></code>

主题

我在我的应用程序中使用了一个Android X主题 - Theme.MaterialComponents.NoActionBar.Bridge

受影响的设备(我所知道的)

  • One Plus 7 Pro (GM1913, GM1915, GM1917)
  • Samsung S9+ 韩国版 (SM-G965N)

1
如果你使用androidx,你也应该使用相应的材料主题。 - ror
1
那么你在活动中没有使用abc_textfield_search_material吗? - Santanu Sur
1
我目前使用桥接主题,因为我使用了一个库(Theme.MaterialComponents.NoActionBar.Bridge),这应该也可以,对吧?它在大多数设备上都能正常工作。而且我没有直接在任何地方使用 abc_textfield_search_material,它是在 SearchView 内部膨胀中访问的(它来自于 SearchView.<init>)。 - prom85
1
你能添加你的依赖项吗? - karan
1
在非常罕见的设备上,我也会遇到崩溃问题... - Islam Salah
显示剩余5条评论
2个回答

1

它抱怨:

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

应该包含在 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 可能是另一个选择;如果可能的话,附上设备详细信息。


正如你所说,androidx 应该包含这个资源 - 所以这个崩溃不应该与设备有关,但显然并非如此。此外,似乎并不是很多开发者都遇到了这个问题,这让我感到困惑 - 我只有不到 10 万用户,但却有不少这样的崩溃... - prom85
我用安卓版本7.1.1的摩托罗拉Nexus 6遇到了同样的问题。 - Salmon K P

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接