Android:PreferenceFragment与自定义首选项生命周期不一致?

Android:PreferenceFragment与自定义首选项生命周期不一致?,android,android-fragments,android-preferences,Android,Android Fragments,Android Preferences,我正在尝试创建要在中显示的自定义项,如下所述:。我的自定义首选项的外观和功能应与相同,但有一个额外的TextView用于错误报告 我实现了所有功能,UI看起来很好,但当我的PreferenceFragment显示时,我无法初始化此首选项 Preference.onBindView()的文档说明: 这是获取布局中自定义视图的引用的好地方 并设置它们的属性 所以我做了: @Override protected void onBindView(View view) { super.onBind

我正在尝试创建要在中显示的自定义项,如下所述:。我的自定义首选项的外观和功能应与相同,但有一个额外的
TextView
用于错误报告

我实现了所有功能,UI看起来很好,但当我的PreferenceFragment显示时,我无法初始化此首选项

Preference.onBindView()的文档说明:

这是获取布局中自定义视图的引用的好地方 并设置它们的属性

所以我做了:

@Override
protected void onBindView(View view) {
    super.onBindView(view);
    txtError = (TextView) view.findViewById(R.id.error);
}

public void setError(String errorMessage) {
    txtError.setText(errorMessage);
    notifyChanged();
}
但是,当我在
PreferenceFragment.onResume()
中调用
CustomSwitchPreference.setError(String)
时,我得到了NPE,因为
txtError
为空

我试图找到一些解决方法,但PreferenceFragment中似乎没有生命周期方法,它保证在所有基础
首选项
视图初始化后调用(我同时选中了
Preference.onBindView(View)
Preference.onCreateView(ViewGroup)

这种行为没有任何意义-当显示
PreferenceFragment
时,应该有某种方法初始化底层
首选项的UI。我怎样才能做到这一点

注意:调用
customPreference.setTitle(String)
customPreference.setSummary(String()
CustomPreferenceFragment.onResume()
中可以很好地工作。这只是额外的
TextView
我找不到的引用

CustomSwitchPreference.java:

public class CustomSwitchPreference extends SwitchPreference {

    private TextView txtError;

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSwitchPreference(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        setLayoutResource(R.layout.custom_switch_preference_layout);
        return super.onCreateView(parent);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        txtError = (TextView) view.findViewById(R.id.error);
    }

    public void setError(String errorMessage) {
        txtError.setText(errorMessage);
        notifyChanged();
    }

}
CustomPreferenceFragment.java:

public class CustomPreferenceFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(PREFERENCES_FILE_NAME);
        addPreferencesFromResource(R.xml.application_settings);
    }


    @Override
    public void onResume() {
        super.onResume();
        Preference preference = findPreference("CUSTOM_PREF");
        if (preference == null ||
                !CustomSwitchPreference.class.isAssignableFrom(preference.getClass()))
            throw new RuntimeException("couldn't get a valid reference to custom preference");

        CustomSwitchPreference customPreference = (CustomSwitchPreference) preference;
        customPreference.setError("error");
    }
}
自定义\u开关\u首选项\u layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@android:id/widget_frame">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"/>

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="3"/>

        <TextView
            android:id="@+id/error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="3"/>

    </LinearLayout>

    <FrameLayout
        android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

应用程序设置.xml:

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

    <com.example.settings.CustomSwitchPreference
        android:key="CUSTOM_PREF"/>

</PreferenceScreen>

我找不到解决这个问题的合适方法-这种不一致性感觉像是AOSP中的生命周期缺陷,但我不能100%确定这一点

作为一种解决方法,我定义了一个回调接口,
CustomSwitchPreference
onBindView
方法中调用该接口,以便通知包含
PreferenceFragment
的对象它已被初始化:

@Override
protected void onBindView(View view) {
    super.onBindView(view);
    txtError = (TextView) view.findViewById(R.id.error);
    initializationListener.onInitialized(CustomSwitchPreference.this);
}
我想在
onResume
中对这个
CustomSwitchPreference
执行的所有操作现在都在
onInitialized
回调中执行。这是一个难看的解决方法,需要大量的样板文件,但似乎是可行的