android - fragment 测试错误 : android. view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>

标签 android android-fragments android-testing android-fragmentscenario

我正在尝试按照以下说明测试 fragment :https://developer.android.com/training/basics/fragments/testing

但是,当我从我的测试中调用 launchFragmentInContainer 时,我遇到了以下崩溃。

堆栈跟踪:

android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>

Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createView(LayoutInflater.java:647)
...
at com.myapp.poll.PollHomeFragment.onCreateView(PollHomeFragment.kt:31)
...
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0301b1 a=-1}
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
at android.view.View.__constructor__(View.java:5010)
at android.view.View.<init>(View.java)
at android.widget.TextView.<init>(TextView.java)
...
at com.myapp.poll.PollHomeFragment.onCreateView(PollHomeFragment.kt:31)

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".poll.PollActivity">

<TextView
    android:id="@+id/tvEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="asdf@asdf.com" />

<TextView                      <!-- This is line 16 -->
    android:id="@+id/tvViewPoll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:gravity="center"
    android:minHeight="48dp"
    android:text="View Poll" />

<TextView
    android:id="@+id/tvCreatePoll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:gravity="center"
    android:minHeight="48dp"
    android:text="Create Poll" />

最佳答案

好的,从基于堆栈跟踪的有根据的猜测中,我尝试删除 android:background="?attr/selectableItemBackground" 并且这解决了问题。

似乎 ?attr/selectableItemBackground 可能与 FragmentScenario 不兼容,或者这是一个框架错误。

我在问题跟踪器上提交了一个错误:https://issuetracker.google.com/issues/144629519

来自 Google 的更新:

Status: Won't Fix (Intended Behavior) The default theme does not have an app defined attribute ?attr/selectableItemBackground (as Fragment has no dependency on AppCompat, MDC, etc)

It sounds like you should be passing the correct theme to FragmentScenario:

launchFragmentInContainer<PollHomeFragment>(themeResId = R.style.Theme_YourTheme)

关于android - fragment 测试错误 : android. view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903155/

相关文章:

java - SQLite数据库更新功能找不到行ID?

android - 调用带有数据绑定(bind)的方法时如何获取 View

java - ListView 不显示在 Fragment 中 (Android)

android - 如何一起使用 MonkeyRunner 和 Robotium(不是分开使用!)

Android - 独立的 Fragment UI 测试工具

android - 将库添加到android studio

android - 很棒的搜索栏

java - android.support.v4.view.ViewPager 在 android.app 包中是否有等效项以及为什么是早期版本?

android - 每个布局有多个 Fragments 的 Backstack

android - 重新运行失败的 android 测试