【发布时间】:2018-07-12 14:36:37
【问题描述】:
我正在尝试将已经设计好的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>
【问题讨论】:
-
MainSettingsFragment 也必须扩展 android.support.v4.app.Fragment
-
发布 MainSettingsFragment 而不是 SettingFragment
标签: android fragment preferencefragment