PreferenceFragment中的RunTimeException

时间:2014-07-25 05:21:01

标签: android android-fragments android-preferences preferenceactivity

我关注this tutorial。其中PreferenceActivity用于创建设置。然而有人提到我可以使用片段,如果我喜欢,所以我继续在正常活动中使用PreferenceFrament。但是当我运行应用程序时,它崩溃了,给出了以下日志 -

07-25 10:41:02.048    7515-7515/com.example.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunshine/com.example.sunshine.SettingsActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
            at android.app.ActivityThread.access$600(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4744)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
            at android.preference.PreferenceFragment.ensureList(PreferenceFragment.java:350)
            at android.preference.PreferenceFragment.getListView(PreferenceFragment.java:336)
            at android.preference.PreferenceFragment.bindPreferences(PreferenceFragment.java:330)
            at android.preference.PreferenceFragment.onActivityCreated(PreferenceFragment.java:171)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:852)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
            at android.app.BackStackRecord.run(BackStackRecord.java:635)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1411)
            at android.app.Activity.performStart(Activity.java:5017)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
            at android.app.ActivityThread.access$600(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4744)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

当我搜索SO时,我发现崩溃与ListFragment有些相关。但我确信我没有在我的应用程序中使用它。 PreferenceFragment可能已经调用了它。我无法在这里查明问题。你能帮我解决一下吗?

这是我正在使用的首选xml -

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <EditTextPreference
        android:title="@string/pref_location_label"
        android:key="@string/pref_location_key"
        android:defaultValue="@string/pref_location_default"
        android:inputType="text"
        android:singleLine="true"
        />
</PreferenceScreen>

这里是SettingsActivity,还有片段 -

public class SettingsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new SettingsFragment())
                    .commit();
        }
    }

    public static class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {

        public SettingsFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Add 'general' preferences, defined in the XML file
            addPreferencesFromResource(R.xml.pref_general);

            // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
            // updated when the preference changes.
            bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_settings, container, false);
            return rootView;
        }

        /**
         * Attaches a listener so the summary is always updated with the preference value.
         * Also fires the listener once, to initialize the summary (so it shows up before the value
         * is changed.)
         */
        private void bindPreferenceSummaryToValue(Preference preference) {
            // Set the listener to watch for value changes.
            preference.setOnPreferenceChangeListener(this);

            // Trigger the listener immediately with the preference's
            // current value.
            onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(preference.getContext())
                            .getString(preference.getKey(), ""));
        }

        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list (since they have separate labels/values).
                ListPreference listPreference = (ListPreference) preference;
                int prefIndex = listPreference.findIndexOfValue(stringValue);
                if (prefIndex >= 0) {
                    preference.setSummary(listPreference.getEntries()[prefIndex]);
                }
            } else {
                // For other preferences, set the summary to the value's simple string representation.
                preference.setSummary(stringValue);
            }
            return true;
        }
    }
}

我还需要更改其他任何内容才能使其适用于Fragments而非Activity吗?

3 个答案:

答案 0 :(得分:4)

终于找到了它。我只需要在我的片段的xml中放置一个带有android:id="@id/android:list"属性的ListView。虽然我不知道为什么我需要它,但只是通过反复试验找到它。

答案 1 :(得分:2)

除了noob的解决方案之外还有一些信息。如果有人想知道为什么要添加WORKS,那是因为在(源代码)偏好片段中 - &gt; bindpreferences(),它实际上尝试在当前视图中查找列表视图(使用android.r.id.list),如果生成的视图不是listview,则会抛出上述异常。此列表对于显示首选项是必需的。

答案 2 :(得分:1)

您的问题似乎在activity_settings.xml,因为它表示您(据说),创建了一个包含以下ID的视图:android.R.id.list并且不是ListView检查您的问题activity_settings.xml您可能会发现问题,否则请发布您的XML以查看是否存在问题。