androidx.preference.PreferenceScreen not found when creating Preferences screen

一个人想着一个人 提交于 2021-01-26 22:00:48

问题


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.

Error inflating class (not found)androidx.preference.PreferenceScreen

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" />

Activity class

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

Fragment class

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

回答1:


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"

The legacy is for the old com.android.support:preference-v14 while the other is for com.android.support:preference-v7.

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




回答2:


if you want to use PreferenceFragmentCompat first you need to implement the following dependency in you app level gradle.

implementation 'androidx.preference:preference:1.0.0



回答3:


With support libraries 28.0.0 you should have XML code like this: (NOTE remove androidx for this case)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
    <CheckBoxPreference
        android:key="preference_a"
        android:defaultValue="false"
        android:title="Preference A" />
</PreferenceScreen>

and gradle config file:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:preference-v7:28.0.0'

and the implementation like this: (NOTE: set instead of add and rootKey):

public class SettingsFragment extends PreferenceFragmentCompat {

    public static final String TAG = SettingsFragment.class.getSimpleName();

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }
}

In the oficial documentation, they have androidx in the sample code, maybe that is the problem, you don't need it with support libraries



来源:https://stackoverflow.com/questions/53615637/androidx-preference-preferencescreen-not-found-when-creating-preferences-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!