【问题标题】:androidx.preference.PreferenceScreen not found when creating Preferences screen创建首选项屏幕时找不到androidx.preference.PreferenceScreen
【发布时间】:2019-05-06 00:55:57
【问题描述】:

在关注this tutorial 为首选项创建屏幕后,膨胀类“androidx.preference.PreferenceScreen”似乎存在问题。为什么在res/xml 文件夹中声明了我的首选项并且已将必要的依赖项添加到该项目时找不到它?

我的应用的 minSdkVersion 是 24。

错误膨胀类(未找到)androidx.preference.PreferenceScreen

依赖项

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>

活动布局

<?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)
    }
}

【问题讨论】:

  • 截至2019/04/10,设置tutorial有明显错误。他们忘记在活动中调用 setContentView。

标签: android xml kotlin android-preferences preferencefragment


【解决方案1】:

如果你使用的是 AndroidX,你应该更新你的依赖:

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

如果您不使用 AndroidX 而是使用 Android 支持库,请不要将 AndroidX 小部件导入您的 XML。

【讨论】:

  • 我的应用的 minSdkVersion 是 24。我还需要使用旧版本吗?请检查我的依赖项列表。
  • 您在我们的 XML 中使用 AndroidX 小部件,但您的依赖项是支持小部件!
  • 好的。这就是原因,但 Android 开发者教程中没有提到这一点。
【解决方案2】:

如果你想首先使用PreferenceFragmentCompat,你需要在你的应用级gradle中实现以下依赖。

implementation 'androidx.preference:preference:1.0.0

【讨论】:

  • 尝试更具体。分析你的答案。
【解决方案3】:

使用支持库28.0.0,您应该有这样的 XML 代码: (注意在这种情况下删除androidx

<?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>

gradle 配置文件:

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

和这样的实现: (注意set 而不是addrootKey):

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);
    }
}

oficial documentation,他们的示例代码中有androidx,这可能是问题所在,你不需要支持库

【讨论】:

  • 代码必须是 Kotlin 而不是 Java
  • 是一样的。点击最后的链接。
  • 我的项目使用 AndroidX
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
相关资源
最近更新 更多