主题参考属性-UI测试

问题描述

我正在尝试通过检测(android)测试来测试片段。该测试的代码如下。出于某种原因,导致测试启动失败的属性主题引用-> ?colorOnSecondary。如果删除属性,则测试将正常启动。

有什么想法吗?

@RunWith(AndroidJUnit4::class)
class MyFragmentTest {
      @Before
        fun init() {
            val appContext = InstrumentationRegistry.getInstrumentation().context
            appContext.setTheme(R.style.AppTheme)
            val scenario = launchFragmentInContainer<TestBlankFragment>()
        }
    
        @Test
        fun mytest() {
    //No code,just first trying to launch page without error.
        }
    }

class TestBlankFragment : Fragment()
{
    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
       // return super.onCreateView(inflater,container,savedInstanceState)
        return inflater.inflate(R.layout.placeholder_fragment,false)
    }
}

placeholder_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
    
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/text"
                android:textAppearance="@style/TextAppearance.AppTheme.heading1"
  />
        </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

themes.xml

    <!--Top level AppTheme theme to be used in AndroidManifest.xml-->
    <style name="AppTheme" parent="Base.AppTheme"/>

    <!--Base custom theme which will be shared between both light and dark theme variants-->
    <style name="Base.AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorSecondary">@color/color_secondary</item>
</resources>

styles.xml

 <style name="TextAppearance.AppTheme.heading1" parent="TextAppearance.MaterialComponents.Headline1">
            <item name="fontFamily">@font/roboto_regular</item>
            <item name="android:textSize">@dimen/heading1_text_size</item>
            <item name="android:textColor">?colorOnSecondary</item>
        </style>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color_secondary">@color/palette_white</color>
</resources>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...