2

请帮帮我,最近两天我一直在碰壁,无法弄清楚。

我有一个简单的 PreferenceFragment 并想从一个活动中膨胀它。

这是 res/xml/preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="preferences">
<PreferenceCategory
        android:title="@string/notification_basic"
        android:key="notification">
    <CheckBoxPreference
            android:key="enable_notification"
            android:summary="@string/enable_notification"
            android:title="@string/notification_title"
            android:defaultValue="false">
    </CheckBoxPreference>
    <CheckBoxPreference
            android:key="enable"
            android:summary="@string/enable_instant_notification"
            android:title="@string/notify_me_instantly"
            android:dependency="enable_notification"
            android:defaultValue="false">
    </CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory
        android:title="@string/sign_up_notify_me_where_section"
        android:key="locations">
    <MultiSelectListPreference
            android:dialogTitle="@string/sign_up_notify_me_where_title"
            android:key="location"
            android:summary="@string/sign_up_notify_me_where"
            android:title="@string/sign_up_notify_me_where_title"
            android:entries="@array/locations"
            android:entryValues="@array/locations_values"
            android:defaultValue="@array/empty_array"
            android:dependency="enable_notification"
            />
</PreferenceCategory>
<PreferenceCategory
        android:title="@string/sign_up_notify_me_type_section"
        android:key="types">
    <MultiSelectListPreference
            android:dialogTitle="@string/sign_up_notify_me_type_title"
            android:key="type"
            android:summary="@string/sign_up_notify_me_type"
            android:title="@string/sign_up_notify_me_type_title"
            android:entries="@array/types"
            android:entryValues="@array/types_values"
            android:defaultValue="@array/empty_array"
            android:dependency="enable_notification"
            />
</PreferenceCategory>
<PreferenceCategory
        android:title="@string/sign_up_notify_me_who_section"
        android:key="units">
    <MultiSelectListPreference
            android:dialogTitle="@string/sign_up_notify_me_who_title"
            android:key="units"
            android:summary="@string/sign_up_notify_me_who"
            android:title="@string/sign_up_notify_me_who_title"
            android:entries="@array/units"
            android:entryValues="@array/units_values"
            android:defaultValue="@array/empty_array"
            android:dependency="enable_notification"
            />
    </PreferenceCategory>
</PreferenceScreen>

活动:

public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, new SignUpPreferenceFragment());
    fragmentTransaction.commit();
    }
}

活动布局:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/home_layout">
<FrameLayout android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:id="@+id/fragment_container"
        />
</RelativeLayout>

最后是堆栈跟踪:

04-05 20:11:07.641: ERROR/AndroidRuntime(5731): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.soid.client.activities/com.soid.client.activities.HomeActivity}:     android.view.InflateException: Binary XML file line #1: Error inflating class     PreferenceScreen
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
    at android.app.ActivityThread.access$600(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5520)
    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:1029)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #1: Error inflating     class PreferenceScreen
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at     com.soid.client.activities.SignUpPreferenceFragment.onCreateView(SignUpPreferenceFragment.ja    va:18)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1037)
    at android.app.BackStackRecord.run(BackStackRecord.java:635)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
    at android.app.Activity.performStart(Activity.java:5075)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
    ... 11 more
    Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
    at    com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    ... 20 more

为什么它会在 android.view 中搜索 PreferenceScreen?我完全迷路了,请帮助我。

编辑:

我忘了粘贴实际的 PreferenceFragment:

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.xml.preferences, container, false);

谢谢!

4

3 回答 3

11

您需要删除您的onCreateView功能,SignUpPreferenceFragment因为您无法使用充气机充气偏好。

这就是为什么您收到错误消息,指出存在错误膨胀PreferenceScreen,因为它不理解 xml 中的标记/类。

的膨胀preferences.xmladdPreferencesFromResourcein完成onCreate

参考

于 2014-04-06T13:58:56.973 回答
1

你必须有一个类来膨胀preference.xml,如下所示:

public static class SignUpPreferenceFragment extends PreferenceFragment {

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

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
}

}

于 2014-04-05T20:09:37.220 回答
0

将偏好主题添加到您的 styles.xml

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

于 2017-10-28T15:06:03.447 回答