Java 如何使用Androidx的PreferenceScreen
我的问题是我想在我的应用程序中添加一个PreferenceScreen,但我不能使用它,我也不知道为什么 我实现了库Java 如何使用Androidx的PreferenceScreen,java,android,xml,android-preferences,androidx,Java,Android,Xml,Android Preferences,Androidx,我的问题是我想在我的应用程序中添加一个PreferenceScreen,但我不能使用它,我也不知道为什么 我实现了库androidx。首选项:首选项:1.1.0-rc01。然后,我想将PreferenceScreen添加到我的XML布局中,但它没有提出任何建议 接下来,我将来自Android开发者的XML代码复制到我的XML布局中并进行编译,但通过启动活动,它会出现错误:java.lang.ClassCastException:class androidx.preference.Preferen
androidx。首选项:首选项:1.1.0-rc01
。然后,我想将PreferenceScreen添加到我的XML布局中,但它没有提出任何建议
接下来,我将来自Android开发者的XML代码复制到我的XML布局中并进行编译,但通过启动活动,它会出现错误:java.lang.ClassCastException:class androidx.preference.PreferenceScreen无法转换为Android.view.view
有人能帮我正确使用androidx.preference.preference屏幕吗
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.preference.SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</androidx.preference.PreferenceScreen>
试试:xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</PreferenceScreen>
java.lang.ClassCastException:classandroidx.preference.PreferenceScreen
不能强制转换为android.view.view
我的布局:
要从XML属性扩展层次结构,请创建、覆盖并提供XML资源:
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
然后将此片段
添加到您的活动中
,就像添加任何其他片段一样。现在,完整的答案是:
将此行添加到你的应用程序Gradle:implementation'androidx。首选项:首选项:1.1.1'
或implementation'androidx。首选项:首选项ktx:1.1.1'
。和同步格雷德尔。创造
res文件夹中名为xml
的目录
在此目录中创建一个带有首选名称的XML文件,例如main\u preferences
。根元素必须是
androidx.preference.PreferenceScreen
用您的设置填充XML文件,例如:
接下来,有两个选项可以在您的应用程序中实现PreferencesSeen
最好的方法是,在创建时按照代码实现它。
在设置活动中添加框架布局
,并为其提供一个ID,例如fl\u main\u设置
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/<!-- yourID -->">
</FrameLayout>
就是这样,玩得开心。你能发布你的版面吗?是的,我能发布我的版面。就是这样。另外,你的偏好活动也会很有帮助。谢谢你的教程,它非常清晰。但是,当你说(第5点)“添加你的框架布局”时,你能说得更清楚些吗?你漏掉了一个词,不是吗?它是否在您的main_activity.xml中添加了框架布局???或者它是在另一个xml文件中?我一直在一个空对象引用上获取“java.lang.NullPointerException:尝试调用虚拟方法'void androidx.recyclerview.widget.recyclerview.setAdapter(androidx.recyclerview.widget.recyclerview$Adapter)”,并且只有在我取消注释该行时才会得到:“setPreferencesFromResource(R.xml.settings,rootKey);”xml看起来很好谢谢你Christian。我无意中评论了它。现在我已经修好了,你在哪里,为什么用回收站,皮特?我不认为这是因为我的代码。也许您可以检查您的RecyclerView适配器及其初始化;)
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen>
...
</androidx.preference.PreferenceScreen>
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
....
</PreferenceScreen>
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.preference.SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</androidx.preference.PreferenceScreen>
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
import com.???.???.R;
public class <YourClassname> extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.<yourXmlFilename>, rootKey);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/<!-- yourID -->">
</FrameLayout>
package com.???.???;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;
import com.???.???.MainSettingsFragment;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.<SettingsActivityXML(with the FrameLayout)>);
//If you want to insert data in your settings
<YourSettingsFragmentClass> settingsFragment = new <YourSettingsFragmentClass>();
settingsFragment. ...
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,settingsFragment).commit();
//Else
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,new <YourSettingsFragmentClass>()).commit();
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<fragment
android:tag="frag"
android:name="com.quickme.musicme.Fragments.MainSettingsFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>