【问题标题】:PreferenceFragment not working after onCreateViewPreferenceFragment 在 onCreateView 后不起作用
【发布时间】:2013-09-07 16:37:36
【问题描述】:

我遇到了这个奇怪的错误:

1 PregerenceFragment 在正常活动中。在片段中有两个 SwitchPreferences。现在一切正常。但是,当我覆盖 onCreateView 方法时,这些开关就像一个开关一样,意味着它们始终具有相同的状态 - 选中/取消选中一个,另一个做同样的事情。

请问这是知道错误还是我做错了什么?

谢谢

偏好xml:

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

    <SwitchPreference
        android:defaultValue="true"
        android:key="@string/key_syncing_wifi"
        android:title="123" />
    <SwitchPreference
        android:defaultValue="false"
        android:key="@string/key_syncing_carrier"
        android:title="234" />

</PreferenceScreen>

片段布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    tools:context=".PreferenceActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="123" />

</LinearLayout>

片段类

public class PreferenceSyncingFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
    }

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

}

【问题讨论】:

    标签: android layout android-fragments android-custom-view preference


    【解决方案1】:

    我无法找出导致该特定行为的原因,但以下显然解决了该问题:

    1. 删除onCreateView()
    2. 将fragment_preference.xml 布局直接添加到preference.xml。

    preference.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <SwitchPreference
            android:defaultValue="true"
            android:key="@string/key_syncing_wifi"
            android:title="123" />
        <SwitchPreference
            android:defaultValue="false"
            android:key="@string/key_syncing_carrier"
            android:title="234" />
    
        <Preference android:layout="@layout/fragment_preference" />
    
    </PreferenceScreen>
    

    此外,CheckBoxPreference 未观察到此类行为。

    希望这会有所帮助。

    【讨论】:

    • 您好,如何查看oncreatemethod 中添加的首选项?
    • 你好@火影忍者!不幸的是,我看不到与 OP 有任何关系。我认为,这值得一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2016-10-01
    相关资源
    最近更新 更多