After following this tutorial to create a screen for preferences, there seems to be a problem with inflating the class 'androidx.preference.PreferenceScreen'. Why is it not found when my preferences have been declared inside the res/xml folder and the necessary dependency has been added to this project?

My app's minSdkVersion is 24.

inflating 类时出错(找不到)androidx.parence.Preference enceScreen

dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:preference-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference
        android:key="preference_a"
        android:defaultValue="false"
        android:title="Preference A" />

</androidx.preference.PreferenceScreen>

Activity layout

<?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:id="@+id/settings_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MySettingsActivity" />

活动课

class MySettingsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings_container, MySettingsFragment())
                .commit()
    }
}

片段类

class MySettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.app_preferences)
    }
}

推荐答案

If you're using AndroidX, you should update your dependencies:

implementation "androidx.legacy:legacy-preference-v14:1.0.0"
implementation "androidx.preference:preference:1.0.0"

遗产是给老com.android.support:preference-v14人的,而另一个是给com.android.support:preference-v7人的.

If you don't use AndroidX but Android Support libraries, do not import AndroidX widgets into your XML.

Kotlin相关问答推荐

有没有一种简单的方法可以将数组/列表中的每个元素相互相乘 - Kotlin?

如何创建扩展函数isNullOrEmpty?

Kotlin 具体类从抽象类和接口扩展而来,接口使用抽象类中实现的方法

kotlin 单例异常是好是坏?

Kotlin:如何使用第一个参数的默认值进行函数调用并为第二个参数传递一个值?

Kotlin 可打包类抛出 ClassNotFoundException

java - 如何将函数作为参数从java传递给kotlin方法?

Swift vs Kotlin 在排序数组上的表现

Kotlin 协程中的 Dispatchers.Main 和 Dispatchers.Default 有什么区别?

如何从不同的功能发出流量值? Kotlin 协程

Kotlin 静态函数:伴生对象,@JvmStatic @JvmField

如何将 kotlin 集合作为 varagrs 传递?

Kotlin boxed Int 不一样

将 @Component.Builder 与构造函数参数一起使用

Kotlin通过映射委托属性,如果映射中不存在,则抛出NoTouchElementException

如何从kotlin中的类实例化对象

参数不匹配;SimpleXML

如何使用协调器布局和行为在CardView上完成此动画?

Kotlin 对象 vs 伴生对象(companion-object) vs 包作用域(package scoped)方法

Android studio,构建kotlin时出现奇怪错误:生成错误代码