错误膨胀类 com.google.android.material.floatingactionbutton.FloatingActionButton Instrumentation Test

Var*_*Raj 2 android android-espresso

如果我正常运行该应用程序,该应用程序工作正常,但是当我尝试测试时,它显示错误,这是错误的完整日志。

internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 由:java.lang.IllegalArgumentException:此组件上的样式需要你的应用主题是 Theme.AppCompat(或一个后代)。在 com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243) 在 com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:213) 在 com.google.android.material .internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:148) 在 com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76) 在 com.google.android.material.floatingactionbutton.FloatingActionButton.(FloatingActionButton. java:211) 在 com.google.android.material.floatingactionbutton。

这是我的 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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="adapter"
            type="com.medium.todoapp.adapters.ToDoListAdapter" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".HomeFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:adapter="@{adapter}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:listitem="@layout/item_todo" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

这是我的测试文件

@RunWith(AndroidJUnit4::class)
class HomeFragmentInstrumentedTest {

    @Test
    fun testRecyclerView() {
        val homeScenario = launchInContainer(HomeFragment::class.java)
        homeScenario.onFragment {
            Assert.assertNotEquals(it.view?.findViewById(R.id.recyclerView), null)
        }
    }

    @Test
    fun testAddTodoButton() {
        val homeScenario = launchInContainer(HomeFragment::class.java)
        homeScenario.onFragment {
            Assert.assertNotEquals(it.view?.findViewById(R.id.addToDo), null)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        bindings = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
        bindings?.apply {
            this.adapter = ToDoListAdapter()
        }
        return bindings?.root
    }
Run Code Online (Sandbox Code Playgroud)

构建.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.medium.todoapp"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled = true
    }

    kapt {
        generateStubs = true
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // AndroidX
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // Test
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // Tests
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'

    // Mockito
    androidTestImplementation "org.mockito:mockito-core:2.24.5"
    androidTestImplementation "org.mockito:mockito-android:2.24.5"

    // FragmentScenario
    debugImplementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
    debugImplementation ("androidx.fragment:fragment-testing:1.2.4") {
        // required, while there is no version 1.2.0 yet.
        exclude group: "androidx.test", module: "core"
    }

    // Navigation Architecture Component
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
}

Run Code Online (Sandbox Code Playgroud)

ata*_*nko 6

尝试替换launchInContainer(HomeFragment::class.java)为:

launchInContainer(HomeFragment::class.java, null, R.style.AppTheme, null)
Run Code Online (Sandbox Code Playgroud)

R.style.AppTheme 必须继承自 Theme.AppCompat