为什么此主题属性会导致我的检测测试崩溃?

  • 本文关键字:测试 崩溃 属性 android
  • 更新时间 :
  • 英文 :


谁能帮我了解导致这次崩溃发生了什么?

Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.widget.CheckedTextView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 17: TypedValue{t=0x2/d=0x7f020060 a=-1}
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:716)

我的仪器测试:

@Test
fun testOnBindViewHolder_setsClickEventForFriendItemView() {
val mockOnClickPersonItemDelegate: PersonListAdapter.OnClickPersonDelegate = mock()
personListAdapter = PersonListAdapter(mockOnClickPersonItemDelegate)
personListAdapter.addOne(getFakePerson())
val inflater: LayoutInflater = InstrumentationRegistry.getInstrumentation().context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val itemView = inflater.inflate(R.layout.person_list_item, null)
val holder = personListAdapter.getViewHolder(itemView)
// verify that a click on the item calls the click event listener
personListAdapter.onBindViewHolder(holder, 0)
holder.itemView.performClick()
verify(mockOnClickPersonItemDelegate).onClickPersonWithId(ArgumentMatchers.anyString())
}

itemView视图 XML 的膨胀会导致此崩溃。但这并不是由于我在XML中定义或引用的dimen。位于 0x7f020060 处的缺失维度不是用户定义的;它是 AppCompat 主题的R.attr.dialogPreferredPadding值。 同样,我没有在 XML 中的任何位置引用CheckedTextView。我试图想象为什么这个视图不能在androidTest上下文中膨胀,因为它可以在main

.

同样的问题,我想我找到了解决方案。尝试用ContextThemeWrapper包装用于创建膨胀的上下文。下面是 Java 中的一个例子:

final Context c = new ContextThemeWrapper(InstrumentationRegistry.getTargetContext(), R.style.Theme_AppCompat);
final LayoutInflater inflater = LayoutInflater.from(c);

除了R.style.Theme_AppCompat,您还可以使用应用程序自定义主题(我猜是从 AppCompat 主题扩展而来的),因此您将能够膨胀包含您在自定义主题中定义的自定义属性的布局。

最新更新