3

我正在尝试按照以下说明测试片段: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" />

4

3 回答 3

4

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

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

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

来自谷歌的更新:

状态:不会修复(预期行为)默认主题没有应用定义的属性 ?attr/selectableItemBackground(因为 Fragment 不依赖于 AppCompat、MDC 等)

听起来您应该将正确的主题传递给 FragmentScenario:

launchFragmentInContainer<PollHomeFragment>(themeResId = R.style.Theme_YourTheme)
于 2019-11-17T17:25:56.800 回答
2

在 Java 中,还必须将主题传递给FragmentScenario. 否则Fragment将不知道当前的主题,最终会抱怨应该设置一个AppCompat主题......

FragmentScenario<SomeFragment> scenario = FragmentScenario.launchInContainer(
    SomeFragment.class,
    Bundle.EMPTY,
    R.style.Theme_Custom,
    null
);
于 2021-02-06T11:34:09.593 回答
0

对我来说,改用“ android: ?attr/selectableItemBackground”解决了这个问题

于 2021-07-29T08:37:38.030 回答