如何将偏好片段实现为现有片段?

时间:2018-07-12 14:36:38

标签: android fragment preferencefragment

我正在尝试将已经设计的preferenceFragment(MainSetting片段)加载到现有片段(设置片段)中,我使用以下命令加载片段:

getFragmentManager().beginTransaction().replace(android.R.id.content,new MainSettingsFragment ()).commit();  

但是代码说:

Wrong 2nd argument type. Found: 'com.example.***.myapplication.SettingFragment.MainSettingsFragment', required: 'android.support.v4.app.Fragment'   

关于打包或导入是否存在任何问题?还是我的加载方法完全错误?这是我的主要代码:

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends Fragment {


public SettingFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content,new 
MainSettingsFragment ()).commit();
 }

public static class MainSettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

}

更新

我通过下载android.support.v4 jar解决了我的问题,通过合并这两个片段看起来还不错,但是当我运行我的应用程序时,它显示了另一个以下问题

    android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class selector
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class selector
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.selector" on path: DexPathList  

我多次检查代码,但我仍然不知道为什么会收到此错误,这是否意味着我应该在我的preferenceFragment xml文件中添加选择器?

最后,我在这里列出了preferenceFragment布局文件

preferences.xml

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

<PreferenceCategory
    android:key="user_category"
    android:title="Sensor Setting"
    >

    <SwitchPreference
        android:defaultValue="true"
        android:key="acc_setting"
        android:summary="Movement information"
        android:title="Acceleration" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="acc1_setting"
        android:summary="Acceleration without gravity"
        android:title="Linear Acceleration" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="gyro_setting"
        android:summary="Angle information"
        android:title="Gyroscope" />
    <SwitchPreference
        android:defaultValue="false"
        android:key="magne_setting"
        android:summary="Magnetic information"
        android:title="Magnetic" />
    <SwitchPreference
        android:defaultValue="false"
        android:key="orien_setting"
        android:summary="Orientation information"
        android:title="Orientation" />
    <SwitchPreference
        android:defaultValue="false"
        android:key="rota_setting"
        android:summary="Rotation information"
        android:title="Rotation" />

</PreferenceCategory>

<PreferenceCategory
    android:key="visua_category"
    android:title="Visualization"
    >

    <CheckBoxPreference
        android:defaultValue="falsee"
        android:key="visual_data"
        android:summary="Real-time visualize sensor data"
        android:title="Visualizing Sensor Information" />

</PreferenceCategory>

1 个答案:

答案 0 :(得分:0)

我认为您应该扩展导入android.support.v4.preference.PreferenceFragment而不是import android.preference.PreferenceFragment;