[英]addPreferencesFromResources, Can't figure it out
Well first of all, sorry I made this post, I know there are others like this and I tried to use them but I can't get it to work. 首先,很抱歉,我发表了这篇文章,我知道还有其他类似的文章,并且我尝试使用它们,但我无法使其正常工作。
I am using the book "Hello, Android", the third edition, and I stumbled upon the deprecation of the addPreferencesFromResources()
method. 我正在使用第三版的《 Hello,Android》,而偶然发现了
addPreferencesFromResources()
方法的弃用。 So I tried to fix it, but for some reason I can't get it to work correctly. 因此,我尝试对其进行修复,但是由于某种原因,我无法使其正常工作。 I have several classes that I believe are used here:
我相信这里有几个类:
Prefs java class Prefs Java类
package com.example.sudoku;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity
{
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
}
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
This one is copied from one of the other posts, but as I said, I can't get it to work and I don't know why. 这篇文章是从其他一篇文章中复制而来的,但是正如我所说,我无法使其正常运行,我也不知道为什么。
Furthermore I have the settings.xml file. 此外,我还有settings.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="@string/music_title"
android:summary="@string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="hints"
android:title="@string/hints_title"
android:summary="@string/hints_summary"
android:defaultValue="true" />
</PreferenceScreen>
And also a little part of my Sudoku java class that calls for the activity with Prefs.class: 还有我的Sudoku Java类的一小部分,它要求使用Prefs.class进行活动:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any) ...
}
return false;
}
我已经修复了它,我所要做的就是将settings.xml文件中的视图实现为Prefs类的内容,尽管我不能使用CheckBoxPreference,而不得不使用CheckBox,但我仍然很高兴关于它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.