片段测试错误:android.view.InflateException:二进制XML文件第16行:二进制XML文件第16行:错误膨胀类
我正试图按照以下说明测试一个片段: 但是,当从测试中调用launchFragmentInContainer时,我遇到以下崩溃 堆栈跟踪:片段测试错误:android.view.InflateException:二进制XML文件第16行:二进制XML文件第16行:错误膨胀类,android,android-fragments,android-testing,android-fragmentscenario,Android,Android Fragments,Android Testing,Android Fragmentscenario,我正试图按照以下说明测试一个片段: 但是,当从测试中调用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 l
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" />
好的,根据stacktrace的推测,我尝试删除android:background=?attr/selectableItemBackground,这就解决了这个问题 看起来?attr/selectableItemBackground可能与FragmentScenario不兼容,或者这是一个框架错误 我在问题追踪器上提交了一个bug: 谷歌更新: 状态:不会修复默认主题没有的预期行为 应用程序定义的属性?attr/selectableItemBackground作为片段 不依赖于AppCompat、MDC等 听起来你应该将正确的主题传递给 碎片场景:
launchFragmentInContainer<PollHomeFragment>(themeResId = R.style.Theme_YourTheme)
在Java中,还必须将主题传递给用户。否则,片段将不知道当前主题,并最终抱怨应该设置AppCompat主题
FragmentScenario<SomeFragment> scenario = FragmentScenario.launchInContainer(
SomeFragment.class,
Bundle.EMPTY,
R.style.Theme_Custom,
null
);
发布测试类代码,检查Textview的id是否在活动中初始化。