在 PreferenceActivity 中找不到自定义首选项的Preference

发布于 2024-12-24 16:16:09 字数 1257 浏览 1 评论 0原文

我制作了一个自定义首选项布局,每行有 2 个切换,称为 Dualtogglepreference。以及一个扩展 Preference 的类,用于处理它的一些细节。当我将此自定义首选项添加到preferences.xml 文件中时,它会出现在 UI 中,但我无法在首选项活动中使用 findPreference 来引用它。

Preferences.xml 文件

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

    <com.hitpost.testing.DualTogglePreference
        android:key="followsMe"
        android:title="Someone follows me"
        android:layout="@layout/dualtogglepreference"/>

</PreferenceCategory>
</PreferenceScreen>

PreferenceActivity

public class TestingCustomPreferenceActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    DualTogglePreference followsMe = (DualTogglePreference) findPreference("followsMe");

    if (followsMe != null)
        Log.e("FOLLOWS_ME", "NOT NULL");
    else
        Log.e("FOLLOWS_ME", "NULL"); //THIS IS PRINTED
}
}

视觉上一切看起来都很完美,即小部件的布局是正确的。请帮忙,最后一天一直在与此作斗争。

I have made a custom preference layout, that has 2 toggles per row, called dualtogglepreference. Along with a class that extends Preference that handles some of the specifics for it. When I add this custom preference to my preferences.xml file it appear in the UI but I am unable to reference it using findPreference in the Preference Activity.

preferences.xml file

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

    <com.hitpost.testing.DualTogglePreference
        android:key="followsMe"
        android:title="Someone follows me"
        android:layout="@layout/dualtogglepreference"/>

</PreferenceCategory>
</PreferenceScreen>

PreferenceActivity

public class TestingCustomPreferenceActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    DualTogglePreference followsMe = (DualTogglePreference) findPreference("followsMe");

    if (followsMe != null)
        Log.e("FOLLOWS_ME", "NOT NULL");
    else
        Log.e("FOLLOWS_ME", "NULL"); //THIS IS PRINTED
}
}

Visually everything looks perfect, ie the layout for the widget is correct. Please help, have been battling this for the last day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-12-31 16:16:09

就我而言,我忽略了定义充气机将使用的构造函数。

public class StaticDialogPreference extends DialogPreference {
    // this constructor is called by the infaltor
    public StaticDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StaticDialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setDialogMessage(getContext().getString(R.string.static_message));
        setNegativeButtonText(null);
    }
}

In my case, I had neglected to define the constructor that will be used by the inflator.

public class StaticDialogPreference extends DialogPreference {
    // this constructor is called by the infaltor
    public StaticDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StaticDialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setDialogMessage(getContext().getString(R.string.static_message));
        setNegativeButtonText(null);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文