Android Studio 0.8.6 - 错误 - PreferenceActivity的子类必须覆盖isValidFragment(String)

时间:2014-08-24 02:56:09

标签: android-fragments android-preferences

只需使用Android Studio,并尝试向其中一个Google UI示例添加偏好活动,但不会走得太远。首先,新建&gt;活动 - >设置活动向导似乎不知道偏好标题标记,因此无法包含@overide:< strong> isValidFragment(String)和 onBuildHeaders(列出目标)在它生成的代码中。无论如何,在将两个示例应用程序混合在一起并添加一个isValidFragment()函数之后,例如。

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
            loadHeadersFromResource(R.xml.pref_headers, target);
    }

    @Override
    protected boolean isValidFragment(String fragmentName) {
        return SettingsActivity.class.getName().equals(fragmentName)
                || super.isValidFragment(fragmentName);
    }

...


public static class DataSyncPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_data_sync);

        // Bind the summaries of EditText/List/Dialog/Ringtone preferences
        // to their values. When their values change, their summaries are
        // updated to reflect the new value, per the Android Design
        // guidelines.
        bindPreferenceSummaryToValue(findPreference("prefSyncToGoogle"));
        bindPreferenceSummaryToValue(findPreference("prefSyncToApple"));
        bindPreferenceSummaryToValue(findPreference("prefSyncToMyFitnessPal"));
    }
}

我仍然收到错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.slidingtabsbasic/com.example.android.slidingtabsbasic.SettingsActivity}: java.lang.RuntimeException: Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid! com.example.android.slidingtabsbasic.SettingsActivity has not checked if fragment com.example.android.slidingtabsbasic.SettingsActivity$DataSyncPreferenceFragment is valid.

其中pref_headers.xml文件为:

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

    <header
        android:fragment="com.example.android.slidingtabsbasic.SettingsHeaders$GeneralPreferenceFragment"
        android:title="@string/pref_header_general" />
    <header
        android:fragment="com.example.android.slidingtabsbasic.SettingsHeaders$NotificationPreferenceFragment"
        android:title="@string/pref_header_notifications" />
    <header
        android:fragment="com.example.android.slidingtabsbasic.SettingsHeaders$DataSyncPreferenceFragment"
        android:title="@string/pref_header_data_sync" />

</preference-headers>

快速调试显示:

确定,

SettingsActivity.class.getName() = "com.example.android.slidingtabsbasic.SettingsActivity"

虽然:

fragmentName  = "com.example.android.slidingtabsbasic.SettingsActivity$DataSyncPreferenceFragment"

所以上面的 isValidFragment()调用显然不会起作用,所以在函数中定义片段名称的静态数组并进行查找的唯一选择是什么?

e.g。

private final String[] FRAGMENTS = {
        "com.example.android.slidingtabsbasic.SettingsActivity$GeneralPreferenceFragment",
        "com.example.android.slidingtabsbasic.SettingsActivity$NotificationPreferenceFragment",
        "com.example.android.slidingtabsbasic.SettingsActivity$DataSyncPreferenceFragment"
};

...

@Override
protected boolean isValidFragment(String fragmentName) {

    for (String FRAGMENT : FRAGMENTS) {
        if (FRAGMENT.equals(fragmentName)) {
            return true;
        }
    }
    return super.isValidFragment(fragmentName);
}

1 个答案:

答案 0 :(得分:0)

请参阅isValidFragment Android API 19Is it possible to get all the subclasses of a class?

出于安全考虑,您必须明确定义它们。你可以考虑解析你的xml片段。